www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - need help to check symbol is static variable or not

reply Dakota <dakota gmail.com> writes:
this code give error:

```d
static if(  !__traits(compiles, mixin("enum v = V;")) ) {
    enum v = V;
}

```

```sh
  Error: static variable `NotWorkVar` cannot be read at compile 
time
```

V is alias get from `typeof(__traits(getMember, M, symbol)`,  M 
is `module` from importC.


the importC source "test.h" code for `NotWorkVar`

```c
extern const float NotWorkVar;
```

I am try to ` static foreach(symbol; __traits(allMembers, 
importCModule))`,  and want to check if the symbol is `static 
variable` or not at compile time.
Jul 01
parent reply Nick Treleaven <nick geany.org> writes:
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 ```
Jul 01
next sibling parent Dakota <dakota gmail.com> writes:
On Monday, 1 July 2024 at 11:15:46 UTC, Nick Treleaven wrote:
 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 ```
Thanks, this work for me.
Jul 01
prev sibling parent reply Dakota <dakota gmail.com> writes:
On Monday, 1 July 2024 at 11:15:46 UTC, Nick Treleaven wrote:
 ```
There is a case check will give wrong resutls: ```d struct type_s { union { struct { int a; int b; } c; }; }; ``` `type c` will return false with `enum isStatic(alias V) = __traits(compiles, { enum v = V; });` type_s is from importC with DMD v2.110.0-beta.1.
Jul 14
parent reply Nick Treleaven <nick geany.org> writes:
On Monday, 15 July 2024 at 06:44:12 UTC, Dakota wrote:
 ```d
 struct type_s
 {

     union {
         struct
         {
             int a;
             int b;
         } c;
     };
 };
 ```

 `type c` will return false with `enum isStatic(alias V) = 
 __traits(compiles, { enum v = V; });`
I put `type_s` in a file anonstruct.c, then: ```d import anonstruct; pragma(msg, isStatic!(type_s.c)); ``` That outputs `false`, because the symbol is not a compile-time value. Is that what you meant?
Jul 15
parent Dakota <dakota gmail.com> writes:
On Monday, 15 July 2024 at 11:16:23 UTC, Nick Treleaven wrote:
 I put `type_s` in a file anonstruct.c, then:
 ```d
 import anonstruct;

 pragma(msg, isStatic!(type_s.c));
 ```
 That outputs `false`, because the symbol is not a compile-time 
 value. Is that what you meant?
No, I am here deal with a lot type and structs, no symbol problem yet. I guess the problem cause by the subtype has a hidden pointer to parent.
Jul 15