Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
September 13, 2015 how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
can I check if a member of a T has a member without using a mixin? hid_t createDataType(T)() if (__traits(isSame, TemplateOf!(T), PriceBar)) { auto tid=H5T.create(H5TClass.Compound,T.sizeof); enum offsetof(alias type, string field) = mixin(type.stringof ~"."~field~".offsetof"); H5T.insert(tid, "date", offsetof!(T,"date"), createDataType!(KalDate)); static if(hasMember!(T,"open")) H5T.insert(tid,"open",offsetof!(T,"open"),mapDtoHDF5Type("double")); } I would like to check if date has a member called second. Thanks. |
September 13, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Sunday, 13 September 2015 at 16:46:54 UTC, Laeeth Isharc wrote:
> can I check if a member of a T has a member without using a mixin?
>
> hid_t createDataType(T)()
> if (__traits(isSame, TemplateOf!(T), PriceBar))
> {
> auto tid=H5T.create(H5TClass.Compound,T.sizeof);
> enum offsetof(alias type, string field) = mixin(type.stringof ~"."~field~".offsetof");
> H5T.insert(tid, "date", offsetof!(T,"date"), createDataType!(KalDate));
> static if(hasMember!(T,"open"))
> H5T.insert(tid,"open",offsetof!(T,"open"),mapDtoHDF5Type("double"));
> }
>
> I would like to check if date has a member called second.
>
> Thanks.
Use __traits(compiles, date.second)?
|
September 13, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to wobbles | 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");
|
September 13, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | 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.
|
September 14, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to BBasile | 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.
|
September 14, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Monday, 14 September 2015 at 14:05:01 UTC, Laeeth Isharc wrote:
> 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:
>>> [...]
>>
>> 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.
You mean hasMember!(typeof(T.bar.date), "hour"), right?
|
September 14, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | On Monday, 14 September 2015 at 14:21:12 UTC, John Colvin wrote:
> On Monday, 14 September 2015 at 14:05:01 UTC, Laeeth Isharc wrote:
>> 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:
>>>> [...]
>>>
>>> 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.
>
> You mean hasMember!(typeof(T.bar.date), "hour"), right?
Ahh. Probably that was why (I will check it shortly). Why do I need to do a typeof? What kind of thing is T.bar.date before the typeof given that T is a type?
|
September 14, 2015 Re: how do I check if a member of a T has a member ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Monday, 14 September 2015 at 15:04:00 UTC, Laeeth Isharc wrote:
> On Monday, 14 September 2015 at 14:21:12 UTC, John Colvin wrote:
>> On Monday, 14 September 2015 at 14:05:01 UTC, Laeeth Isharc wrote:
>>> 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:
>>>>> [...]
>>>>
>>>> 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.
>>
>> You mean hasMember!(typeof(T.bar.date), "hour"), right?
>
> Ahh. Probably that was why (I will check it shortly). Why do I need to do a typeof? What kind of thing is T.bar.date before the typeof given that T is a type?
T.bar.date is just a symbol. If you tried to actually access it then it would have to be a compile-time construct or be a static member/method, but it's perfectly OK to ask what type it has or what size it has.
The simple story: hasMember takes a type as its first argument. T.bar.date isn't a type, it's a member of a member of a type. To find out what type it is, use typeof.
|
Copyright © 1999-2021 by the D Language Foundation