static assert(0) in template is a disaster

Dennis dkorpel at gmail.com
Tue Jun 16 22:16:34 UTC 2020


On Tuesday, 16 June 2020 at 18:14:26 UTC, Nils Lankila wrote:
>> template example(T)
>> {
>>     static assert(__traits(hasMember, T, "x"));
>>     alias example = T.x;
>> }
>>
>> struct S
>> {
>>     static if (!__traits(compiles, example!S)) {
>>         int x;
>>     }
>> }
>>
>> static assert(is(typeof(example!S) == int));
>
> that's unfortunate, looks like the problem cant be solved then.

In my opinion, the fact that that compiles is a blatant bug. 
However, the D specification is not thorough enough to 
conclusively say. Even if we define it better and patch the 
compiler, unfortunately the paradoxical "if this doesn't compile 
yet, make it compile" pattern has been adopted in the wild so it 
would be a breaking change. To see why it's problematic, ask 
yourself why this compiles:

```
static assert(x == 3);
static if (!is(typeof(x))) immutable x = 3;
static if (!is(typeof(x))) immutable x = 5;
```

I've written about this in more detail before:
https://forum.dlang.org/post/tlpogchogwsopswgbpdu@forum.dlang.org





More information about the Digitalmars-d mailing list