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

2015-05-03 Thread Meta via Digitalmars-d-learn

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 instantiating


Try 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.


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

2015-05-03 Thread ketmar via Digitalmars-d-learn
On Sun, 03 May 2015 17:21:58 +, 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.

signature.asc
Description: PGP signature


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

2015-05-03 Thread ketmar via Digitalmars-d-learn
On Sun, 03 May 2015 18:02:37 +, 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, 
so you'd better read one of the D books to better understand my cryptic 
don't do that, do this comments. ;-)

signature.asc
Description: PGP signature


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

2015-05-03 Thread filcuc via Digitalmars-d-learn

On Sunday, 3 May 2015 at 17:48:55 UTC, ketmar wrote:

On Sun, 03 May 2015 17:21:58 +, 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!



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

2015-05-03 Thread filcuc via Digitalmars-d-learn

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