static assert not printing out along the error diagnostic

Paul Backus snarwin at gmail.com
Wed Jul 14 02:42:21 UTC 2021


On Wednesday, 14 July 2021 at 02:28:07 UTC, someone wrote:
> If I add in main():
>
> ```d
>     auto g = MyType!string();
>     g.doWork();
> ```
>
> I get:
>
> ...: Error: undefined identifier `ResultType`
> ...: Error: template instance `...MyType!string` error 
> instantiating
>
> But nothing like:
>
> ```d
> T.stringof ~ " is not supported"
> ```

Weirdly, adding a dummy `ResultType` declaration to the `else` 
branch makes the assert message show up:

     } else {
         alias ResultType = void;
         static assert(false, T.stringof ~ " is not supported");
     }

https://run.dlang.io/is/68sM0Z

Somehow the compiler is processing the declaration of `doWork` 
before the `static assert` statement, even though it comes later 
in the source code.

According to run.dlang.io this behavior was introduced in DMD 
2.066.0, so the example in the book must not have been updated 
for quite a while.


More information about the Digitalmars-d-learn mailing list