Skip to content

Commit 938fd8c

Browse files
author
sprinkle131313
committed
Add trait for testing if a member is static.
1 parent 2935ae0 commit 938fd8c

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

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)
@@ -3549,6 +3550,176 @@ enum hasMember(T, string name) = __traits(hasMember, T, name);
35493550
static assert(hasMember!(S, "foo"));
35503551
}
35513552

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

0 commit comments

Comments
 (0)