digitalmars.D.learn - UDA and ReturnType!(__traits...) doesn't work
Hi all, i'm working in the generation of the code but i'm failing in extracting a function return type when invoking the ReturnType!(T) type trait and mixing it with __traits(getMember) function. Can anyone help me? or explaining what is happenning? i've created a gist here: https://gist.github.com/filcuc/14c3a6cac89beb69cccd The error message is the following: main.d-mixin-58(58): Error: template instance main.Person.ReturnType!(name) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) main.d-mixin-58(58): Error: template instance std.traits.ReturnType!(Monitor) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) main.d(73): Error: template instance main.IterateUDA!(Person) error instantiating
May 03 2015
On Sunday, 3 May 2015 at 17:22:00 UTC, filcuc wrote:Hi all, i'm working in the generation of the code but i'm failing in extracting a function return type when invoking the ReturnType!(T) type trait and mixing it with __traits(getMember) function. Can anyone help me? or explaining what is happenning? i've created a gist here: https://gist.github.com/filcuc/14c3a6cac89beb69cccd The error message is the following: main.d-mixin-58(58): Error: template instance main.Person.ReturnType!(name) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) main.d-mixin-58(58): Error: template instance std.traits.ReturnType!(Monitor) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) main.d(73): Error: template instance main.IterateUDA!(Person) error instantiatingTry adding the following around your 'string returnType = ...' code: static if(isSomeFunction!(__traits(get member, T, member)) { ... } The problem is that you're looping over all the fields in T, including the data members which obviously don't have a return type. I guess ReturnType should probably use a static assert to give a better error message.
May 03 2015
On Sun, 03 May 2015 17:21:58 +0000, filcuc wrote:Hi all, i'm working in the generation of the code but i'm failing in extracting a function return type when invoking the ReturnType!(T) type trait and mixing it with __traits(getMember) function. Can anyone help me? or explaining what is happenning? i've created a gist here: https://gist.github.com/filcuc/14c3a6cac89beb69cccd =20 The error message is the following: =20 main.d-mixin-58(58): Error: template instance main.Person.ReturnType!(name) does not match template declaration ReturnType(func...) if (func.length =3D=3D 1 && isCallable!func) main.d-mixin-58(58): Error: template instance std.traits.ReturnType!(Monitor) does not match template declaration ReturnType(func...) if (func.length =3D=3D 1 && isCallable!func) main.d(73): Error: template instance main.IterateUDA!(Person) error instantiatingthe thing is that you mixed CTFE and runtime code in a weird way. ;-) the following works: void IterateUDA(T)() { import std.typetuple : staticIndexOf; foreach (member; __traits(allMembers, T)) { // Check that the given member is a function static if (is(typeof(__traits(getMember, T, member)))) { bool isFunction =3D __traits(isVirtualFunction, __traits(getMember,=20 T, member)); if (!isFunction) continue; // Retrieve the UDA enum attributes =3D __traits(getAttributes, __traits(getMember, T,=20 member)); // Extract the Function Return Type and Arguments if `Slot()` is=20 found static if (staticIndexOf!(Slot(), attributes) >=3D 0) { enum returnType =3D ReturnType!(__traits(getMember, T,=20 member)).stringof; import std.stdio; writeln(returnType); } } } } yet i think you need to read more about CTFE and metaprogramming, to=20 avoid mixing different code.=
May 03 2015
On Sunday, 3 May 2015 at 17:48:55 UTC, ketmar wrote:On Sun, 03 May 2015 17:21:58 +0000, filcuc wrote:Yep sorry, i'm still learning :) However thank you all for your help and the quick answer. As you suggested wrapping the body with the static if solved the problem. I've updated the gist https://gist.github.com/filcuc/14c3a6cac89beb69cccd Thanks!Hi all, i'm working in the generation of the code but i'm failing in extracting a function return type when invoking the ReturnType!(T) type trait and mixing it with __traits(getMember) function. Can anyone help me? or explaining what is happenning? i've created a gist here: https://gist.github.com/filcuc/14c3a6cac89beb69cccd The error message is the following: main.d-mixin-58(58): Error: template instance main.Person.ReturnType!(name) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) main.d-mixin-58(58): Error: template instance std.traits.ReturnType!(Monitor) does not match template declaration ReturnType(func...) if (func.length == 1 && isCallable!func) main.d(73): Error: template instance main.IterateUDA!(Person) error instantiatingthe thing is that you mixed CTFE and runtime code in a weird way. ;-) the following works: void IterateUDA(T)() { import std.typetuple : staticIndexOf; foreach (member; __traits(allMembers, T)) { // Check that the given member is a function static if (is(typeof(__traits(getMember, T, member)))) { bool isFunction = __traits(isVirtualFunction, __traits(getMember, T, member)); if (!isFunction) continue; // Retrieve the UDA enum attributes = __traits(getAttributes, __traits(getMember, T, member)); // Extract the Function Return Type and Arguments if `Slot()` is found static if (staticIndexOf!(Slot(), attributes) >= 0) { enum returnType = ReturnType!(__traits(getMember, T, member)).stringof; import std.stdio; writeln(returnType); } } } } yet i think you need to read more about CTFE and metaprogramming, to avoid mixing different code.
May 03 2015
On Sun, 03 May 2015 18:02:37 +0000, filcuc wrote:Yep sorry, i'm still learning :)i'm not blaming you at all. what i mean i that i'm bad at explanations,=20 so you'd better read one of the D books to better understand my cryptic=20 "don't do that, do this" comments. ;-)=
May 03 2015