Skip to content

Commit 7c81234

Browse files
committed
Implement issue# 16485. Add trait for testing if a member is static.
isStaticMember tests whether a member of a struct or class is static or not. It works with both functions and variables.
1 parent bf010cd commit 7c81234

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

std/traits.d

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* $(LREF hasElaborateDestructor)
4444
* $(LREF hasIndirections)
4545
* $(LREF hasMember)
46+
* $(LREF isStaticMember)
4647
* $(LREF hasNested)
4748
* $(LREF hasUnsharedAliasing)
4849
* $(LREF InterfacesTuple)
@@ -3340,6 +3341,171 @@ enum hasMember(T, string name) = __traits(hasMember, T, name);
33403341
static assert(hasMember!(S, "foo"));
33413342
}
33423343

3344+
/++
3345+
Whether the symbol represented by the string, member, is a static member of
3346+
T.
3347+
+/
3348+
template isStaticMember(T, string member)
3349+
if (__traits(hasMember, T, member))
3350+
{
3351+
import std.meta : Alias;
3352+
import std.traits : FunctionTypeOf;
3353+
alias sym = Alias!(__traits(getMember, T, member));
3354+
3355+
static if (is(typeof(sym) == function) || is(FunctionTypeOf!(typeof(&sym)) == function))
3356+
enum bool isStaticMember = __traits(isStaticFunction, sym);
3357+
else
3358+
enum bool isStaticMember = __traits(compiles, &sym);
3359+
}
3360+
3361+
///
3362+
unittest
3363+
{
3364+
static struct S
3365+
{
3366+
static void sf() {}
3367+
void f() {}
3368+
3369+
static int si;
3370+
int i;
3371+
}
3372+
3373+
static assert( isStaticMember!(S, "sf"));
3374+
static assert(!isStaticMember!(S, "f"));
3375+
3376+
static assert( isStaticMember!(S, "si"));
3377+
static assert(!isStaticMember!(S, "i"));
3378+
3379+
static assert(!__traits(compiles, isStaticMember!(S, "hello")));
3380+
}
3381+
3382+
unittest
3383+
{
3384+
static struct S
3385+
{
3386+
enum X = 10;
3387+
enum Y
3388+
{
3389+
i = 10
3390+
}
3391+
struct S {}
3392+
class C {}
3393+
3394+
static int sx = 0;
3395+
__gshared int gx = 0;
3396+
3397+
Y y;
3398+
static Y sy;
3399+
3400+
static void f() {}
3401+
static void f2() pure nothrow @nogc @safe {}
3402+
3403+
shared void g() {}
3404+
3405+
static void function() fp;
3406+
__gshared void function() gfp;
3407+
void function() fpm;
3408+
3409+
void delegate() dm;
3410+
static void delegate() sd;
3411+
3412+
void m() {}
3413+
final void m2() const pure nothrow @nogc @safe {}
3414+
3415+
inout(int) iom() inout { return 10; }
3416+
static inout(int) iosf(inout int x) { return x; }
3417+
3418+
@property int p() { return 10; }
3419+
static @property int sp() { return 10; }
3420+
}
3421+
3422+
static class C
3423+
{
3424+
enum X = 10;
3425+
enum Y
3426+
{
3427+
i = 10
3428+
}
3429+
struct S {}
3430+
class C {}
3431+
3432+
static int sx = 0;
3433+
__gshared int gx = 0;
3434+
3435+
Y y;
3436+
static Y sy;
3437+
3438+
static void f() {}
3439+
static void f2() pure nothrow @nogc @safe {}
3440+
3441+
shared void g() {}
3442+
3443+
static void function() fp;
3444+
__gshared void function() gfp;
3445+
void function() fpm;
3446+
3447+
void delegate() dm;
3448+
static void delegate() sd;
3449+
3450+
void m() {}
3451+
final void m2() const pure nothrow @nogc @safe {}
3452+
3453+
inout(int) iom() inout { return 10; }
3454+
static inout(int) iosf(inout int x) { return x; }
3455+
3456+
@property int p() { return 10; }
3457+
static @property int sp() { return 10; }
3458+
}
3459+
3460+
static assert(!isStaticMember!(S, "X"));
3461+
static assert(!isStaticMember!(S, "Y"));
3462+
static assert(!__traits(compiles, isStaticMember!(S, "Y.i")));
3463+
static assert(!isStaticMember!(S, "S"));
3464+
static assert(!isStaticMember!(S, "C"));
3465+
static assert( isStaticMember!(S, "sx"));
3466+
static assert( isStaticMember!(S, "gx"));
3467+
static assert(!isStaticMember!(S, "y"));
3468+
static assert( isStaticMember!(S, "sy"));
3469+
static assert( isStaticMember!(S, "f"));
3470+
static assert( isStaticMember!(S, "f2"));
3471+
static assert(!isStaticMember!(S, "dm"));
3472+
static assert( isStaticMember!(S, "sd"));
3473+
static assert(!isStaticMember!(S, "g"));
3474+
static assert( isStaticMember!(S, "fp"));
3475+
static assert( isStaticMember!(S, "gfp"));
3476+
static assert(!isStaticMember!(S, "fpm"));
3477+
static assert(!isStaticMember!(S, "m"));
3478+
static assert(!isStaticMember!(S, "m2"));
3479+
static assert(!isStaticMember!(S, "iom"));
3480+
static assert( isStaticMember!(S, "iosf"));
3481+
static assert(!isStaticMember!(S, "p"));
3482+
static assert( isStaticMember!(S, "sp"));
3483+
3484+
static assert(!isStaticMember!(C, "X"));
3485+
static assert(!isStaticMember!(C, "Y"));
3486+
static assert(!__traits(compiles, isStaticMember!(C, "Y.i")));
3487+
static assert(!isStaticMember!(C, "S"));
3488+
static assert(!isStaticMember!(C, "C"));
3489+
static assert( isStaticMember!(C, "sx"));
3490+
static assert( isStaticMember!(C, "gx"));
3491+
static assert(!isStaticMember!(C, "y"));
3492+
static assert( isStaticMember!(C, "sy"));
3493+
static assert( isStaticMember!(C, "f"));
3494+
static assert( isStaticMember!(C, "f2"));
3495+
static assert(!isStaticMember!(S, "dm"));
3496+
static assert( isStaticMember!(S, "sd"));
3497+
static assert(!isStaticMember!(C, "g"));
3498+
static assert( isStaticMember!(C, "fp"));
3499+
static assert( isStaticMember!(C, "gfp"));
3500+
static assert(!isStaticMember!(C, "fpm"));
3501+
static assert(!isStaticMember!(C, "m"));
3502+
static assert(!isStaticMember!(C, "m2"));
3503+
static assert(!isStaticMember!(C, "iom"));
3504+
static assert( isStaticMember!(C, "iosf"));
3505+
static assert(!isStaticMember!(C, "p"));
3506+
static assert( isStaticMember!(C, "sp"));
3507+
}
3508+
33433509
/**
33443510
Retrieves the members of an enumerated type $(D enum E).
33453511

0 commit comments

Comments
 (0)