www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - ReturnType and Parameters of Templated function/method

reply Patric Dexheimer <patric.dexheimer gmail.com> writes:
There is a way to capture the return type/parameters of a 
templated function like:

void add(T)(T t){}
Parameters!add;

Yes, i know that the template don´t have any type until 
explicitly coded like:
Parameters!(add!int);

Or another solution like getting the string function declaration 
will be enough:
"void add(T)(T t)"

Like what happens with:

void other_add(int x){}
writeln( typeof(__traits(getMember, Module, 
"other_add")).stringof ); //output: void(int x)
Oct 13 2016
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 10/13/2016 07:19 AM, Patric Dexheimer wrote:
 There is a way to capture the return type/parameters of a templated
 function like:

 void add(T)(T t){}
 Parameters!add;

 Yes, i know that the template don´t have any type until explicitly coded
 like:
 Parameters!(add!int);

 Or another solution like getting the string function declaration will be
 enough:
 "void add(T)(T t)"

 Like what happens with:

 void other_add(int x){}
 writeln( typeof(__traits(getMember, Module, "other_add")).stringof );
 //output: void(int x)
There are several related options in std.traits. What exactly are you trying to achieve? Perhaps there is a better way. Ali
Oct 13 2016
parent reply Patric Dexheimer <patric.dexheimer gmail.com> writes:
On Thursday, 13 October 2016 at 18:01:25 UTC, Ali Çehreli wrote:
 On 10/13/2016 07:19 AM, Patric Dexheimer wrote:
 There is a way to capture the return type/parameters of a 
 templated
 function like:

 void add(T)(T t){}
 Parameters!add;

 Yes, i know that the template don´t have any type until 
 explicitly coded
 like:
 Parameters!(add!int);

 Or another solution like getting the string function 
 declaration will be
 enough:
 "void add(T)(T t)"

 Like what happens with:

 void other_add(int x){}
 writeln( typeof(__traits(getMember, Module, 
 "other_add")).stringof );
 //output: void(int x)
There are several related options in std.traits. What exactly are you trying to achieve? Perhaps there is a better way. Ali
I´m working on a Lua binding for D. One of my ideas is to try to make automatic binding for structs, even if some method have templated arguments. My idea is to at least try to bind templated functions with basic types automatically (in the example, bind then add!int, add!float, add!string etc..) I tried arity!add which should resolve my problem too, but did´nt compile too. So for now my idea is to brute force the numbers of arguments with 'compiles' trait or trying to get the sourcecode somehow.
Oct 13 2016
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
On Thursday, 13 October 2016 at 20:52:09 UTC, Patric Dexheimer 
wrote:
 So for now my idea is to brute force the numbers of arguments 
 with 'compiles' trait or trying to get the sourcecode somehow.
depending on source code form (even if you can get it) is highly error-prone. consider it UB. also, i think that you'd better not guess, but ask the user to explicitly instantiate the methods with `alias` -- this way you have to write less hacky code, and the user will have more control over what is bound. you may provide helper mixin templates to instantiate n-ary functions with given set of types too. this is slightly more work on the user side, but it doesn't depend on hacks and undocumented things.
Oct 13 2016
parent Patric Dexheimer <patric.dexheimer gmail.com> writes:
On Thursday, 13 October 2016 at 21:07:17 UTC, ketmar wrote:
 On Thursday, 13 October 2016 at 20:52:09 UTC, Patric Dexheimer 
 wrote:
 So for now my idea is to brute force the numbers of arguments 
 with 'compiles' trait or trying to get the sourcecode somehow.
depending on source code form (even if you can get it) is highly error-prone. consider it UB. also, i think that you'd better not guess, but ask the user to explicitly instantiate the methods with `alias` -- this way you have to write less hacky code, and the user will have more control over what is bound. you may provide helper mixin templates to instantiate n-ary functions with given set of types too. this is slightly more work on the user side, but it doesn't depend on hacks and undocumented things.
Yes, but i like the idea of "automagically" bind everything with little user effort. Sometimes you have a lot of code to expose to lua (and templated code as well with lots of templated argument combinations), so I think is nice to have some automated work on this. But you are right about the "highly error-prone" part. I´ll try to avoid that path :)
Oct 13 2016