how do I check if a member of a T has a member ?
Laeeth Isharc via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Sep 14 07:05:00 PDT 2015
On Sunday, 13 September 2015 at 17:34:11 UTC, BBasile wrote:
> On Sunday, 13 September 2015 at 17:24:20 UTC, Laeeth Isharc
> wrote:
>> On Sunday, 13 September 2015 at 17:09:57 UTC, wobbles wrote:
>>> Use __traits(compiles, date.second)?
>>
>>
>> Thanks.
>>
>> This works:
>>
>> static if (__traits(compiles, { T bar; bar.date.hour;}))
>> pragma(msg,"hour");
>> else
>> pragma(msg,"nohour");
>>
>
> can't you use 'hasMember' (either with __traits() or
> std.traits.hasMember)? It's more idiomatic than checking if
> it's compilable.
I'll check again in a bit, but I seem to recall hasMember didn't
work. I would like to get the type of a member of a type, and I
think hasMember!(T.bar.date","hour") didn't work for that.
Possibly it does work and I messed it up somehow, or it doesn't
work and there is a more elegant way.
Someone ought to write a tutorial showing how to use the good
stuff we have to solve real problems. Eg an annotated babysteps
version of Andrei's allocator talk. I can't do it as too much on
my plate.
More information about the Digitalmars-d-learn
mailing list