UDA and ReturnType!(__traits...) doesn't work
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun May 3 10:48:55 PDT 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
>
> 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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150503/7826dfe3/attachment.sig>
More information about the Digitalmars-d-learn
mailing list