www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Inspecting lambda parameters

reply Jacob Carlborg <doob me.com> writes:
I know that there are templates to inspect function parameters, like 
ParameterIdentifierTuple and ParameterTypeTuple. But these don't work 
for templated/untyped lambdas, they're apparently not callables. I don't 
expect ParameterTypeTuple to work, but it would be nice if 
ParameterIdentifierTuple and "arity" worked.

Here's an example for clarify:

void foo (alias func) ()
{
     alias Types = ParameterTypeTuple!(func);
}

void main ()
{
     foo!(x => x * 2);
}

Anyone know if this is fixable or if there's a workaround? I would like 
to avoid using hacks like .stringof. I know there's __parameters as 
well, but that doesn't work either.

-- 
/Jacob Carlborg
May 10 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Saturday, 10 May 2014 at 10:56:57 UTC, Jacob Carlborg wrote:
 I know that there are templates to inspect function parameters, 
 like ParameterIdentifierTuple and ParameterTypeTuple. But these 
 don't work for templated/untyped lambdas, they're apparently 
 not callables. I don't expect ParameterTypeTuple to work, but 
 it would be nice if ParameterIdentifierTuple and "arity" worked.

 Here's an example for clarify:

 void foo (alias func) ()
 {
     alias Types = ParameterTypeTuple!(func);
 }

 void main ()
 {
     foo!(x => x * 2);
 }

 Anyone know if this is fixable or if there's a workaround? I 
 would like to avoid using hacks like .stringof. I know there's 
 __parameters as well, but that doesn't work either.
Wasn't there recently a pull request to add TemplateArgsOf, or something like that. Also, if you know what type the lambda is going to be instantiated with, you can turn it into a function by doing: void foo (alias func) () { alias Types = ParameterTypeTuple!(func!int); }
May 10 2014
parent Jacob Carlborg <doob me.com> writes:
On 2014-05-10 18:56, Meta wrote:

 Wasn't there recently a pull request to add TemplateArgsOf, or something
 like that.
There's this pull request [1] that adds a couple of new traits that might help.
 Also, if you know what type the lambda is going to be
 instantiated with, you can turn it into a function by doing:

 void foo (alias func) ()
 {
      alias Types = ParameterTypeTuple!(func!int);
 }
Unfortunately I don't know the types it's going to be instantiated with. That's part of the introspecting to figure out. [1] https://github.com/D-Programming-Language/dmd/pull/3515 -- /Jacob Carlborg
May 10 2014