digitalmars.D.learn - Whats the best way to get a struct/class member type?
- simendsjo (5/5) Mar 31 2012 Seems __traits doesn't have a __traits(getMemberType, T, name).
- Jacob Carlborg (4/9) Mar 31 2012 You could use .tupleof, but I don't think that would be any better.
- =?utf-8?Q?Simen_Kj=C3=A6r=C3=A5s?= (7/12) Apr 01 2012 I'd think so, apart from tupleof, as Jacob's already mentioned.
-
simendsjo
(9/23)
Apr 02 2012
On Mon, 02 Apr 2012 00:04:58 +0200, Simen Kj=C3=A6r=C3=A5s
- Simen Kjaeraas (4/29) Apr 02 2012 {
- simendsjo (9/41) Apr 02 2012 =
- simendsjo (2/5) Apr 02 2012 http://d.puremagic.com/issues/show_bug.cgi?id=7809
- Simen Kjaeraas (11/18) Apr 02 2012 struct Foo {
- simendsjo (4/25) Apr 02 2012 It's convenient that it works this way.
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
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
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
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:r, =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
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
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=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#getMemberT t; alias typeof(__traits(getMember, t, name)) getMemberType; }Tested and works for me under 2.058 with just the type.
Apr 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#getMemberhttp://d.puremagic.com/issues/show_bug.cgi?id=7809
Apr 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: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.The documentation explicitly says it shouldn't work for other than static members though. http://dlang.org/traits.html#getMemberhttp://d.puremagic.com/issues/show_bug.cgi?id=7809
Apr 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:It's convenient that it works this way. Probably a change that didn't get reflected to the documentation.On Mon, 02 Apr 2012 10:56:41 +0200, simendsjo <simendsjo gmail.com> wrote: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.The documentation explicitly says it shouldn't work for other than static members though. http://dlang.org/traits.html#getMemberhttp://d.puremagic.com/issues/show_bug.cgi?id=7809
Apr 02 2012