UDA and ReturnType!(__traits...) doesn't work

filcuc via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun May 3 11:02:37 PDT 2015


On Sunday, 3 May 2015 at 17:48:55 UTC, ketmar wrote:
> 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
>> 
>> 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
>
> the 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.


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!



More information about the Digitalmars-d-learn mailing list