Limitations of __traits(compiles, {})

Per Nordlöw per.nordlow at gmail.com
Thu Jul 23 13:33:02 UTC 2020


Why doesn't

     static assert(!__traits(compiles, {
         void f() { int[2] _ = [1, 2]; foreach (const auto ref e; 
_) {} }
     }));

compile when

     static assert(__traits(compiles, {
         void f() { int[2] _ = [1, 2]; foreach (const ref e; _) {} 
}
     }));

does?

Doesn't __traits(compiles) support syntactic changes to the 
language that hasn't yet been implemented?

If so, is there some other way of detecting this?

I want these to automatically detect and signal with a failing 
`static assert` when a new language feature has been implemented 
or preview-activated so it can be used.

For comparison see

/** Is `true` iff DIP-1000 checking is enabled via compiler flag 
-dip1000.
  *
  * See_Also: 
https://forum.dlang.org/post/qglynupcootocnnnpmhj@forum.dlang.org
  * See_Also: 
https://forum.dlang.org/post/pzddsrwhfvcopfaamvak@forum.dlang.org
  */
enum isDIP1000 = __traits(compiles, () @safe { int x; int* p; p = 
&x; });

which can be used to optionally static tests using 
__traits(compiles) when -dip1000 has been fed to the compiler.


More information about the Digitalmars-d mailing list