On Mon, 02 Apr 2012 10:51:35 +0200, Simen Kjaeraas <simen.kja...@gmail.com> wrote:

On Mon, 02 Apr 2012 09:58:18 +0200, simendsjo <simend...@gmail.com> wrote:

On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kjærås <simen.kja...@gmail.com> wrote:

On Sat, 31 Mar 2012 15:20:42 +0200, simendsjo <simend...@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

Reply via email to