RFC: Change what assert does on error

kdevel kdevel at vogtner.de
Wed Jul 2 16:44:09 UTC 2025


On Tuesday, 1 July 2025 at 21:39:04 UTC, H. S. Teoh wrote:
> On Tue, Jul 01, 2025 at 07:18:42PM +0000, Kagamin via 
> Digitalmars-d wrote:
>> On Sunday, 29 June 2025 at 20:58:36 UTC, Richard (Rikki) 
>> Andrew Cattermole
>> wrote:
>> > 4. assert is a framework level error mechanism, not "this 
>> > process can't continue if its false". We'll need something 
>> > else for the latter, it can be library code however.
>> 
>> ```
>> landmine(a.length!=0);
>> ```
>> Not sure how long should be the name.
>
> Just learn from Perl:
>
> 	# Perl
> 	die if ($a != 0);
>
> 	# Proposed D
> 	die(a.length != 0);

That is actually how you throw an exception in Perl. Catch it with
eval and examine the thrown object in $@:

    eval {
        die "something interesting happend"; # "throw"
    };
    if ($@) {                                # "catch"
        warn "caught $@\n";
    }

You may even instruct the runtime to generate a stack dump:

    use Carp;
    $SIG{__DIE__} = 'confess';

Since Perl is refcounted there is no weird magically confused 
runtime
after an exception has been thrown.


More information about the Digitalmars-d mailing list