Challenge

Manu via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 4 05:48:27 PDT 2016


On 4 October 2016 at 14:40, Jonathan M Davis via Digitalmars-d
<digitalmars-d at puremagic.com> wrote:
> On Tuesday, October 04, 2016 14:24:59 Manu via Digitalmars-d wrote:
>> On 4 October 2016 at 12:30, Jonathan M Davis via Digitalmars-d
>>
>> <digitalmars-d at puremagic.com> wrote:
>> > On Tuesday, October 04, 2016 11:13:36 Manu via Digitalmars-d wrote:
>> >> I'm feeling John's solution is a little bit simpler. But nice work,
>> >> thanks!
>> >
>> > So, it is. LOL. I'd actually glanced over that post while I was in the
>> > middle of getting my version to work, and I read it too quickly, because I
>> > understood that it had just solved the property problem and that it didn't
>> > work for all cases. I'll have to update my PR. Though his code does make
>> > the mistake of doing
>> >
>> > mixin(`alias mem = T.` ~ member ~ `;`);
>> >
>> > rather than doing something like
>> >
>> > alias mem = AliasSeq!(__traits(getMember, T, member))[0];
>> >
>> > which means that there are some cases where it won't work properly. The
>> > core logic is simpler though, which is definitely a plus.
>>
>> Make that change in your PR :)
>
> I already updated it and was actually able to make it slightly simpler than
> John's example (as far as I can tell, FunctionTypeOf is only needed in the
> case where the address is taken).
>
>> I think the PR is important. It's not obvious how to do this, and it's
>> very useful. I was astonished it's not already there.
>
> Yeah. I ran into a need for something similar recently, but my
> implementation at the time wasn't as thorough, since it just used offsetof
> to do the check (though in my case, I think that was enough). Getting it
> completely right is surprisingly difficult.
>
> I was also surprised that while we have quite a few __traits for functions,
> they're severely lacking for variables (e.g. I was looking for the variable
> equivalent of __traits(isStaticFunction, ...), and there is no such beast).
> For that matter, even testing whether something is a variable is
> surprisingly difficult.
>
> - Jonathan M Davis

While you're at it, might I suggest also adding std.traits.isProperty?
 Something like:

template isProperty(T, string member)
{
  import std.meta : AliasSeq;
  import std.traits : FunctionTypeOf;
  alias sym = AliasSeq!(__traits(getMember, T, member))[0];
  enum isProperty = !is(typeof(sym) == function) &&
is(FunctionTypeOf!(typeof(&sym)) == function);
}


More information about the Digitalmars-d mailing list