Skip to content

Commit eee2785

Browse files
author
sprinkle131313
committed
Issue 16485 - Add trait for testing if a member is static.
1 parent 93222f0 commit eee2785

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Added `std.traits.isStaticMember` to check whether a symbol is a static member of a type.
2+
-------
3+
import std.traits : isStaticMember;
4+
5+
struct S
6+
{
7+
static int globalVar;
8+
int localVar;
9+
}
10+
11+
assert( isStaticMember!(S, "globalVar"));
12+
assert(!isStaticMember!(S, "localVar"));
13+
-------

std/traits.d

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* $(LREF hasElaborateDestructor)
4545
* $(LREF hasIndirections)
4646
* $(LREF hasMember)
47+
* $(LREF isStaticMember)
4748
* $(LREF hasNested)
4849
* $(LREF hasUnsharedAliasing)
4950
* $(LREF InterfacesTuple)
@@ -3553,6 +3554,176 @@ enum hasMember(T, string name) = __traits(hasMember, T, name);
35533554
static assert(hasMember!(S, "foo"));
35543555
}
35553556

3557+
/**
3558+
* Whether the symbol represented by the string, member, is a static member of T.
3559+
*
3560+
* Params:
3561+
* T = Type containing symbol $(D member).
3562+
* member = Name of symbol to test that resides in $(D T).
3563+
*
3564+
* Returns:
3565+
* $(D true) iff $(D member) is static.
3566+
*/
3567+
template isStaticMember(T, string member)
3568+
if (__traits(hasMember, T, member))
3569+
{
3570+
import std.meta : Alias;
3571+
alias sym = Alias!(__traits(getMember, T, member));
3572+
3573+
static if (__traits(getOverloads, T, member).length == 0)
3574+
enum bool isStaticMember = __traits(compiles, &sym);
3575+
else
3576+
enum bool isStaticMember = __traits(isStaticFunction, sym);
3577+
}
3578+
3579+
///
3580+
unittest
3581+
{
3582+
static struct S
3583+
{
3584+
static void sf() {}
3585+
void f() {}
3586+
3587+
static int si;
3588+
int i;
3589+
}
3590+
3591+
static assert( isStaticMember!(S, "sf"));
3592+
static assert(!isStaticMember!(S, "f"));
3593+
3594+
static assert( isStaticMember!(S, "si"));
3595+
static assert(!isStaticMember!(S, "i"));
3596+
3597+
static assert(!__traits(compiles, isStaticMember!(S, "hello")));
3598+
}
3599+
3600+
unittest
3601+
{
3602+
static struct S
3603+
{
3604+
enum X = 10;
3605+
enum Y
3606+
{
3607+
i = 10
3608+
}
3609+
struct S {}
3610+
class C {}
3611+
3612+
static int sx = 0;
3613+
__gshared int gx = 0;
3614+
3615+
Y y;
3616+
static Y sy;
3617+
3618+
static void f() {}
3619+
static void f2() pure nothrow @nogc @safe {}
3620+
3621+
shared void g() {}
3622+
3623+
static void function() fp;
3624+
__gshared void function() gfp;
3625+
void function() fpm;
3626+
3627+
void delegate() dm;
3628+
static void delegate() sd;
3629+
3630+
void m() {}
3631+
final void m2() const pure nothrow @nogc @safe {}
3632+
3633+
inout(int) iom() inout { return 10; }
3634+
static inout(int) iosf(inout int x) { return x; }
3635+
3636+
@property int p() { return 10; }
3637+
static @property int sp() { return 10; }
3638+
}
3639+
3640+
static class C
3641+
{
3642+
enum X = 10;
3643+
enum Y
3644+
{
3645+
i = 10
3646+
}
3647+
struct S {}
3648+
class C {}
3649+
3650+
static int sx = 0;
3651+
__gshared int gx = 0;
3652+
3653+
Y y;
3654+
static Y sy;
3655+
3656+
static void f() {}
3657+
static void f2() pure nothrow @nogc @safe {}
3658+
3659+
shared void g() {}
3660+
3661+
static void function() fp;
3662+
__gshared void function() gfp;
3663+
void function() fpm;
3664+
3665+
void delegate() dm;
3666+
static void delegate() sd;
3667+
3668+
void m() {}
3669+
final void m2() const pure nothrow @nogc @safe {}
3670+
3671+
inout(int) iom() inout { return 10; }
3672+
static inout(int) iosf(inout int x) { return x; }
3673+
3674+
@property int p() { return 10; }
3675+
static @property int sp() { return 10; }
3676+
}
3677+
3678+
static assert(!isStaticMember!(S, "X"));
3679+
static assert(!isStaticMember!(S, "Y"));
3680+
static assert(!__traits(compiles, isStaticMember!(S, "Y.i")));
3681+
static assert(!isStaticMember!(S, "S"));
3682+
static assert(!isStaticMember!(S, "C"));
3683+
static assert( isStaticMember!(S, "sx"));
3684+
static assert( isStaticMember!(S, "gx"));
3685+
static assert(!isStaticMember!(S, "y"));
3686+
static assert( isStaticMember!(S, "sy"));
3687+
static assert( isStaticMember!(S, "f"));
3688+
static assert( isStaticMember!(S, "f2"));
3689+
static assert(!isStaticMember!(S, "dm"));
3690+
static assert( isStaticMember!(S, "sd"));
3691+
static assert(!isStaticMember!(S, "g"));
3692+
static assert( isStaticMember!(S, "fp"));
3693+
static assert( isStaticMember!(S, "gfp"));
3694+
static assert(!isStaticMember!(S, "fpm"));
3695+
static assert(!isStaticMember!(S, "m"));
3696+
static assert(!isStaticMember!(S, "m2"));
3697+
static assert(!isStaticMember!(S, "iom"));
3698+
static assert( isStaticMember!(S, "iosf"));
3699+
static assert(!isStaticMember!(S, "p"));
3700+
static assert( isStaticMember!(S, "sp"));
3701+
3702+
static assert(!isStaticMember!(C, "X"));
3703+
static assert(!isStaticMember!(C, "Y"));
3704+
static assert(!__traits(compiles, isStaticMember!(C, "Y.i")));
3705+
static assert(!isStaticMember!(C, "S"));
3706+
static assert(!isStaticMember!(C, "C"));
3707+
static assert( isStaticMember!(C, "sx"));
3708+
static assert( isStaticMember!(C, "gx"));
3709+
static assert(!isStaticMember!(C, "y"));
3710+
static assert( isStaticMember!(C, "sy"));
3711+
static assert( isStaticMember!(C, "f"));
3712+
static assert( isStaticMember!(C, "f2"));
3713+
static assert(!isStaticMember!(S, "dm"));
3714+
static assert( isStaticMember!(S, "sd"));
3715+
static assert(!isStaticMember!(C, "g"));
3716+
static assert( isStaticMember!(C, "fp"));
3717+
static assert( isStaticMember!(C, "gfp"));
3718+
static assert(!isStaticMember!(C, "fpm"));
3719+
static assert(!isStaticMember!(C, "m"));
3720+
static assert(!isStaticMember!(C, "m2"));
3721+
static assert(!isStaticMember!(C, "iom"));
3722+
static assert( isStaticMember!(C, "iosf"));
3723+
static assert(!isStaticMember!(C, "p"));
3724+
static assert( isStaticMember!(C, "sp"));
3725+
}
3726+
35563727
/**
35573728
Retrieves the members of an enumerated type $(D enum E).
35583729

0 commit comments

Comments
 (0)