digitalmars.D.learn - need help to check symbol is static variable or not
- Dakota (19/19) Jul 01 this code give error:
- Nick Treleaven (10/16) Jul 01 __traits(compiles, ...) can't check if a declaration is valid
- Dakota (2/20) Jul 01 Thanks, this work for me.
- Dakota (17/18) Jul 14 There is a case check will give wrong resutls:
- Nick Treleaven (8/22) Jul 15 I put `type_s` in a file anonstruct.c, then:
- Dakota (5/12) Jul 15 No, I am here deal with a lot type and structs, no symbol problem
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
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
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:Thanks, this work for me.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
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
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
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