halt with optional message?

Timon Gehr timon.gehr at gmx.ch
Thu Aug 11 06:43:23 PDT 2011


bearophile wrote:
> Is it possible, and is it a good/wise idea to modify D a bit so that in
> release mode this:
>
> assert(0);
>
> becomes a HLT, while in release mode this
>
> assert(0, "message");
>
> Becomes the same thing as:
>
> core.stdc.stdlib.puts("message");
> HTL
>
> ?
>
> What I asking here is support for optional messages in assert(0) too, and
> if it's a good idea.
>
> Note: writing puts before an assert(0) in D code is not the same thing,
> because you are not supposed to be able to use puts in a pure function:
>
> void foo() pure {
>     puts("message");
>     assert(0);
> }
>
> Bye,
> bearophile

I don't think it is a very common need to still have a message for assert(0) when
turning off other assertions.

But you can solve your problem by circumventing the type system:

void assert0impl(string msg){
    writeln(msg);
    assert(0);
}
void assert0(string msg)pure nothrow @trusted{
    (cast(void function(string)pure nothrow @trusted)&assert0impl)(msg);
}

Maybe a better solution would be to have @noreturn functions in the language. (or,
prettier, a type of which no values exist).

Cheers,
-Timon




More information about the Digitalmars-d mailing list