www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Whats the best way to get a struct/class member type?

reply simendsjo <simendsjo gmail.com> writes:
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?
Mar 31 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
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
Mar 31 2012
prev sibling next sibling parent =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= <simen.kjaras gmail.com> writes:
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; }
Apr 01 2012
prev sibling next sibling parent simendsjo <simendsjo gmail.com> writes:
On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kj=C3=A6r=C3=A5s <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(getMembe=

 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; }
Apr 02 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 02 Apr 2012 09:58:18 +0200, simendsjo <simendsjo gmail.com> wrot=
e:

 On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kj=C3=A6r=C3=A5s  =

 <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.
Apr 02 2012
prev sibling next sibling parent simendsjo <simendsjo gmail.com> writes:
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=C3=A6r=C3=A5s  =


 <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 stati= c = members though. http://dlang.org/traits.html#getMember
Apr 02 2012
prev sibling next sibling parent simendsjo <simendsjo gmail.com> writes:
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
Apr 02 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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.
Apr 02 2012
prev sibling parent simendsjo <simendsjo gmail.com> writes:
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.
Apr 02 2012