Thread overview
need help to check symbol is static variable or not
Jul 01, 2024
Dakota
Jul 01, 2024
Nick Treleaven
Jul 02, 2024
Dakota
Jul 15, 2024
Dakota
Jul 15, 2024
Nick Treleaven
Jul 15, 2024
Dakota
July 01, 2024

this code give error:

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

 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

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.

July 01, 2024

On Monday, 1 July 2024 at 07:32:30 UTC, Dakota wrote:

>

this code give error:

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:

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
July 02, 2024

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:

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:

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.

July 15, 2024

On Monday, 1 July 2024 at 11:15:46 UTC, Nick Treleaven wrote:

>

There is a case check will give wrong resutls:

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.

July 15, 2024

On Monday, 15 July 2024 at 06:44:12 UTC, Dakota wrote:

>
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:

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?

July 15, 2024

On Monday, 15 July 2024 at 11:16:23 UTC, Nick Treleaven wrote:

>

I put type_s in a file anonstruct.c, then:

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.