need help to check symbol is static variable or not

Nick Treleaven nick at geany.org
Mon Jul 1 11:15:46 UTC 2024


On Monday, 1 July 2024 at 07:32:30 UTC, Dakota wrote:
> this code give error:
>
> ```d
> static if(  !__traits(compiles, mixin("enum v = V;")) ) {
>    enum v = V;
> }
>
> ```

__traits(compiles, ...) can't check if a declaration is valid 
directly. You have to wrap it in a function literal expression:
```d
enum isStatic(alias V) = __traits(compiles, { enum v = V; });

extern const float NotWorkVar;
const float Works = 2;

pragma(msg, isStatic!NotWorkVar); // false
pragma(msg, isStatic!Works); // true
```


More information about the Digitalmars-d-learn mailing list