static assert("nothing")

Andrea Fontana nospam at example.com
Tue May 31 09:35:30 UTC 2022


On Tuesday, 31 May 2022 at 08:51:45 UTC, realhet wrote:
> Hi,
>
> In my framework I just found a dozen of compile time error 
> handling like:
>
> ...else static assert("Invalid type");
>
> This compiles without error. And it was useless for detecting 
> errors because I forgot the first "false" or "0" parameter.

The first is the assert condition, the message is opt.

> I think it is because of the weird case of "every string casted 
> to bool is true".

string s1;
string s2 = string.init; // Equivalent
string s3 = null; // Equivalent

string s4 = "";

assert(s1, "This fails");
assert(s2, "This fails too");
assert(s3, "This fails too");
assert(s4, "This pass");

> There is an example in Phobos also:  
> https://github.com/dlang/phobos/blob/master/std/uni/package.d
> at line 8847: static assert("Unknown normalization form "~norm);
>
> It is easy to make this mistake, but does static assert(string) 
> has any meaningful use cases?

Check if that string is init.

Andrea




More information about the Digitalmars-d-learn mailing list