static assert("nothing")

JG someone at somewhere.com
Tue May 31 09:11:41 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.
>
> I think it is because of the weird case of "every string casted 
> to bool is true".
>
> 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?

I was going to suggest to do something like:

```d
import std;

string compileError(string msg) {
   import std.format;
   return format("static assert(0,%(%s%));",[msg]);
}

auto doGreatThings(T)(T x)
{
     static if(is(T==int))
     {
         return "great things!";
     }
     else mixin(compileError("Invalid type."));
}

void main()
{
    doGreatThings!int(123).writeln;
    doGreatThings!string("oh dear").writeln;
}
```

But (a) why should you need to and (b) this makes the message 
more obscure.

onlineapp.d-mixin-14(14): Error: static assert:  "Invalid type."
onlineapp.d(20):        instantiated from here: 
`doGreatThings!string`


More information about the Digitalmars-d-learn mailing list