Thread overview
Whats the best way to get a struct/class member type?
Mar 31, 2012
simendsjo
Mar 31, 2012
Jacob Carlborg
Apr 01, 2012
Simen Kjærås
Apr 02, 2012
simendsjo
Apr 02, 2012
Simen Kjaeraas
Apr 02, 2012
simendsjo
Apr 02, 2012
simendsjo
Apr 02, 2012
Simen Kjaeraas
Apr 02, 2012
simendsjo
March 31, 2012
Seems __traits doesn't have a __traits(getMemberType, T, name).
Now I'm doing the following:
T t; // instance to use in getMember
alias typeof( __traits(getMember, t, name) ) MemberType;

Is this the only way to get the type of a field based on the name?
March 31, 2012
On 2012-03-31 15:20, simendsjo wrote:
> Seems __traits doesn't have a __traits(getMemberType, T, name).
> Now I'm doing the following:
> T t; // instance to use in getMember
> alias typeof( __traits(getMember, t, name) ) MemberType;
>
> Is this the only way to get the type of a field based on the name?

You could use .tupleof, but I don't think that would be any better.

-- 
/Jacob Carlborg
April 01, 2012
On Sat, 31 Mar 2012 15:20:42 +0200, simendsjo <simendsjo@gmail.com> wrote:

> Seems __traits doesn't have a __traits(getMemberType, T, name).
> Now I'm doing the following:
> T t; // instance to use in getMember
> alias typeof( __traits(getMember, t, name) ) MemberType;
>
> Is this the only way to get the type of a field based on the name?

I'd think so, apart from tupleof, as Jacob's already mentioned.

It's easily factored out, though:

template getMemberType(T, string name) if (is(typeof(__traits(getMember, T, name)))) {
    alias typeof(__traits(getMember, T, name)) getMemberType;
}
April 02, 2012
On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kjærås <simen.kjaras@gmail.com> wrote:

> On Sat, 31 Mar 2012 15:20:42 +0200, simendsjo <simendsjo@gmail.com> wrote:
>
>> Seems __traits doesn't have a __traits(getMemberType, T, name).
>> Now I'm doing the following:
>> T t; // instance to use in getMember
>> alias typeof( __traits(getMember, t, name) ) MemberType;
>>
>> Is this the only way to get the type of a field based on the name?
>
> I'd think so, apart from tupleof, as Jacob's already mentioned.
>
> It's easily factored out, though:
>
> template getMemberType(T, string name) if (is(typeof(__traits(getMember, T, name)))) {
>      alias typeof(__traits(getMember, T, name)) getMemberType;
> }


getMember requires an instance, not a type, so it should be

template getMemberType(T, string name) if(__traits(hasMember, T, name) {
    T t;
    alias typeof(__traits(getMember, t, name)) getMemberType;
}
April 02, 2012
On Mon, 02 Apr 2012 09:58:18 +0200, simendsjo <simendsjo@gmail.com> wrote:

> On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kjærås <simen.kjaras@gmail.com> wrote:
>
>> On Sat, 31 Mar 2012 15:20:42 +0200, simendsjo <simendsjo@gmail.com> wrote:
>>
>>> Seems __traits doesn't have a __traits(getMemberType, T, name).
>>> Now I'm doing the following:
>>> T t; // instance to use in getMember
>>> alias typeof( __traits(getMember, t, name) ) MemberType;
>>>
>>> Is this the only way to get the type of a field based on the name?
>>
>> I'd think so, apart from tupleof, as Jacob's already mentioned.
>>
>> It's easily factored out, though:
>>
>> template getMemberType(T, string name) if (is(typeof(__traits(getMember, T, name)))) {
>>      alias typeof(__traits(getMember, T, name)) getMemberType;
>> }
>
>
> getMember requires an instance, not a type, so it should be
>
> template getMemberType(T, string name) if(__traits(hasMember, T, name) {
>      T t;
>      alias typeof(__traits(getMember, t, name)) getMemberType;
> }

Tested and works for me under 2.058 with just the type.
April 02, 2012
On Mon, 02 Apr 2012 10:51:35 +0200, Simen Kjaeraas <simen.kjaras@gmail.com> wrote:

> On Mon, 02 Apr 2012 09:58:18 +0200, simendsjo <simendsjo@gmail.com> wrote:
>
>> On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kjærås <simen.kjaras@gmail.com> wrote:
>>
>>> On Sat, 31 Mar 2012 15:20:42 +0200, simendsjo <simendsjo@gmail.com> wrote:
>>>
>>>> Seems __traits doesn't have a __traits(getMemberType, T, name).
>>>> Now I'm doing the following:
>>>> T t; // instance to use in getMember
>>>> alias typeof( __traits(getMember, t, name) ) MemberType;
>>>>
>>>> Is this the only way to get the type of a field based on the name?
>>>
>>> I'd think so, apart from tupleof, as Jacob's already mentioned.
>>>
>>> It's easily factored out, though:
>>>
>>> template getMemberType(T, string name) if (is(typeof(__traits(getMember, T, name)))) {
>>>      alias typeof(__traits(getMember, T, name)) getMemberType;
>>> }
>>
>>
>> getMember requires an instance, not a type, so it should be
>>
>> template getMemberType(T, string name) if(__traits(hasMember, T, name) {
>>      T t;
>>      alias typeof(__traits(getMember, t, name)) getMemberType;
>> }
>
> Tested and works for me under 2.058 with just the type.

You're right - works on 2.059 trunk too..

The documentation explicitly says it shouldn't work for other than static members though.
http://dlang.org/traits.html#getMember
April 02, 2012
On Mon, 02 Apr 2012 10:56:41 +0200, simendsjo <simendsjo@gmail.com> wrote:

>
> The documentation explicitly says it shouldn't work for other than static members though.
> http://dlang.org/traits.html#getMember

http://d.puremagic.com/issues/show_bug.cgi?id=7809
April 02, 2012
On Mon, 02 Apr 2012 12:07:38 +0200, simendsjo <simendsjo@gmail.com> wrote:

> On Mon, 02 Apr 2012 10:56:41 +0200, simendsjo <simendsjo@gmail.com> wrote:
>
>>
>> The documentation explicitly says it shouldn't work for other than static members though.
>> http://dlang.org/traits.html#getMember
>
> http://d.puremagic.com/issues/show_bug.cgi?id=7809

struct Foo {
    int n;
}

void main( ) {
    static assert(is(typeof(Foo.n) == int));
}

Yup, it compiles. Mayhap it shouldn't, but it does. Of course, trying
to use it for anything but typeof gives you an error. From this we can
conclude that either this behavior is also buggy, or getMember should
work that way.
April 02, 2012
On Mon, 02 Apr 2012 12:22:16 +0200, Simen Kjaeraas <simen.kjaras@gmail.com> wrote:

> On Mon, 02 Apr 2012 12:07:38 +0200, simendsjo <simendsjo@gmail.com> wrote:
>
>> On Mon, 02 Apr 2012 10:56:41 +0200, simendsjo <simendsjo@gmail.com> wrote:
>>
>>>
>>> The documentation explicitly says it shouldn't work for other than static members though.
>>> http://dlang.org/traits.html#getMember
>>
>> http://d.puremagic.com/issues/show_bug.cgi?id=7809
>
> struct Foo {
>      int n;
> }
>
> void main( ) {
>      static assert(is(typeof(Foo.n) == int));
> }
>
> Yup, it compiles. Mayhap it shouldn't, but it does. Of course, trying
> to use it for anything but typeof gives you an error. From this we can
> conclude that either this behavior is also buggy, or getMember should
> work that way.

It's convenient that it works this way.
Probably a change that didn't get reflected to the documentation.