Thread overview
Challenge
Oct 03, 2016
Manu
Oct 03, 2016
Andrea Fontana
Oct 03, 2016
Temtaime
Oct 03, 2016
John Colvin
Oct 04, 2016
Manu
Oct 03, 2016
Seb
Oct 03, 2016
Ali Çehreli
October 03, 2016
Fill in the blank...
I'm having a really hard time with this. I've made it work with a
mountain of code, and I want to see what others come up with...

If you succeed, put it in std.traits!

Recommend, use latest DMD nightly. I find differences with latest nightly vs release.

-------------------------------------------------------------------------------
template isStaticMember(T, string member)
{
  enum bool isStaticMember = [code goes here];
}


struct S
{
  enum X = 10;
  enum Y
  {
    i = 10
  }
  struct S {}
  class C {}

  static int x = 0;
  __gshared int y = 0;
  static void f() {}
  static void f2() pure nothrow @nogc @safe {}

  shared void g() {}

  static void function() fp;
  __gshared void function() gfp;
  void function() fpm;

  void m() {}
  final void m2() const pure nothrow @nogc @safe {}

  inout(int) iom() inout { return 10; }
  static inout(int) iosf(inout int x) { return x; }

  @property int p() { return 10; }
  static @property int sp() { return 10; }
}

class C
{
  enum X = 10;
  enum Y
  {
    i = 10
  }
  struct S {}
  class C {}

  static int x = 0;
  __gshared int y = 0;
  static void f() {}
  static void f2() pure nothrow @nogc @safe {}

  shared void g() {}

  static void function() fp;
  __gshared void function() gfp;
  void function() fpm;

  void m() {}
  final void m2() const pure nothrow @nogc @safe {}

  inout(int) iom() inout { return 10; }
  static inout(int) iosf(inout int x) { return x; }

  @property int p() { return 10; }
  static @property int sp() { return 10; }
}

static assert(!isStaticMember!(S, "X"), "!");
static assert(!isStaticMember!(S, "Y"), "!");
static assert(!isStaticMember!(S, "Y.i"), "!");
static assert(!isStaticMember!(S, "S"), "!");
static assert(!isStaticMember!(S, "C"), "!");
static assert( isStaticMember!(S, "x"), "!");
static assert( isStaticMember!(S, "y"), "!");
static assert( isStaticMember!(S, "f"), "!");
static assert( isStaticMember!(S, "f2"), "!");
static assert(!isStaticMember!(S, "g"), "!");
static assert( isStaticMember!(S, "fp"), "!");
static assert( isStaticMember!(S, "gfp"), "!");
static assert(!isStaticMember!(S, "fpm"), "!");
static assert(!isStaticMember!(S, "m"), "!");
static assert(!isStaticMember!(S, "m2"), "!");
static assert(!isStaticMember!(S, "iom"), "!");
static assert( isStaticMember!(S, "iosm"), "!");
static assert(!isStaticMember!(S, "p"), "!");
static assert( isStaticMember!(S, "sp"), "!");

static assert(!isStaticMember!(C, "X"), "!");
static assert(!isStaticMember!(C, "Y"), "!");
static assert(!isStaticMember!(C, "Y.i"), "!");
static assert(!isStaticMember!(C, "S"), "!");
static assert(!isStaticMember!(C, "C"), "!");
static assert( isStaticMember!(C, "x"), "!");
static assert( isStaticMember!(C, "y"), "!");
static assert( isStaticMember!(C, "f"), "!");
static assert( isStaticMember!(C, "f2"), "!");
static assert(!isStaticMember!(C, "g"), "!");
static assert( isStaticMember!(C, "fp"), "!");
static assert( isStaticMember!(C, "gfp"), "!");
static assert(!isStaticMember!(C, "fpm"), "!");
static assert(!isStaticMember!(C, "m"), "!");
static assert(!isStaticMember!(C, "m2"), "!");
static assert(!isStaticMember!(C, "iom"), "!");
static assert( isStaticMember!(C, "iosm"), "!");
static assert(!isStaticMember!(C, "p"), "!");
static assert( isStaticMember!(C, "sp"), "!");
October 03, 2016
On Monday, 3 October 2016 at 13:19:19 UTC, Manu wrote:
> Fill in the blank...
> I'm having a really hard time with this. I've made it work with a
> mountain of code, and I want to see what others come up with...
>
> If you succeed, put it in std.traits!

Pretty easy:
template isStaticMember(T, string member)
{
  enum bool isStaticMember =
	!(member=="X" || member=="Y" || member=="Y.i"  || member=="S"  || member=="C" ||
	member=="g"  || member=="fpm" || member=="m" || member=="m2" || member=="iom"  || member=="p");
}

Ok, I'm joking.


October 03, 2016
On Monday, 3 October 2016 at 13:19:19 UTC, Manu wrote:
> Fill in the blank...
> I'm having a really hard time with this. I've made it work with a
> mountain of code, and I want to see what others come up with...
>
> [...]

Dere's a typo

static assert( isStaticMember!(S, "iosm"), "!");

Should be iosf

Easy:

template isStaticMember(T, string member)
{
	static if(__traits(compiles, &__traits(getMember, T, member)))
	{
		static if(is(FunctionTypeOf!(__traits(getMember, T, member)) == function))
		{
			enum isStaticMember = isFunctionPointer!(__traits(getMember, T, member)) || isDelegate!(__traits(getMember, T, member)) || __traits(isStaticFunction, __traits(getMember, T, member));
		}
		else
		{
			enum isStaticMember = true;//!is(typeof(__traits(getMember, T, member).offsetof));
		}
	}
	else
	{
		enum isStaticMember = false;
	}
}
October 03, 2016
On Monday, 3 October 2016 at 13:19:19 UTC, Manu wrote:
> Fill in the blank...
> I'm having a really hard time with this. I've made it work with a
> mountain of code, and I want to see what others come up with...

template isStaticMember(T, string member)
{
    mixin(`alias mem = T.` ~ member ~ `;`);
    import std.traits : FunctionTypeOf;
    static if (is(FunctionTypeOf!mem == function) && is(FunctionTypeOf!(typeof(&mem)) == function))
        enum bool isStaticMember = __traits(isStaticFunction, mem);
    else
        enum bool isStaticMember = is(typeof(&mem));
}

Basically, using FunctionTypeOf catches @property functions (which just typeof wouldn't), but it also catches member variables with function types, so we need the second FunctionTypeOf to see if it's still a function when you take its address (true for member functions, including @property functions, not true for member variables with function types).

Everything else is just "can you take the address of this".
October 03, 2016
On Monday, 3 October 2016 at 13:19:19 UTC, Manu wrote:
> Fill in the blank...
> I'm having a really hard time with this. I've made it work with a
> mountain of code, and I want to see what others come up with...
>
> If you succeed, put it in std.traits!
>
> Recommend, use latest DMD nightly. I find differences with latest nightly vs release.

See also:

http://stackoverflow.com/questions/39430684/check-if-class-member-is-static
October 03, 2016
On 10/03/2016 07:41 AM, Seb wrote:
> On Monday, 3 October 2016 at 13:19:19 UTC, Manu wrote:
>> Fill in the blank...
>> I'm having a really hard time with this. I've made it work with a
>> mountain of code, and I want to see what others come up with...
>>
>> If you succeed, put it in std.traits!
>>
>> Recommend, use latest DMD nightly. I find differences with latest
>> nightly vs release.
>
> See also:
>
> http://stackoverflow.com/questions/39430684/check-if-class-member-is-static

Appeared on dlang forum as well: :)

  http://forum.dlang.org/post/nni8lp$1ihk$1@digitalmars.com

Ali

October 04, 2016
On 4 October 2016 at 00:25, John Colvin via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> On Monday, 3 October 2016 at 13:19:19 UTC, Manu wrote:
>>
>> Fill in the blank...
>> I'm having a really hard time with this. I've made it work with a
>> mountain of code, and I want to see what others come up with...
>
>
> template isStaticMember(T, string member)
> {
>     mixin(`alias mem = T.` ~ member ~ `;`);
>     import std.traits : FunctionTypeOf;
>     static if (is(FunctionTypeOf!mem == function) &&
> is(FunctionTypeOf!(typeof(&mem)) == function))
>         enum bool isStaticMember = __traits(isStaticFunction, mem);
>     else
>         enum bool isStaticMember = is(typeof(&mem));
> }
>
> Basically, using FunctionTypeOf catches @property functions (which just typeof wouldn't), but it also catches member variables with function types, so we need the second FunctionTypeOf to see if it's still a function when you take its address (true for member functions, including @property functions, not true for member variables with function types).
>
> Everything else is just "can you take the address of this".

Very nice. This is the quality of solution I was looking for! 3 Significant LOC. I knew a simple solution must exist. I didn't think of FunctionTypeOf.