View mode: basic / threaded / horizontal-split · Log in · Help
May 16, 2012
Re: New Traits
On Wednesday, 16 May 2012 at 17:08:43 UTC, Philippe Sigaud wrote:
> If you give it a module name (qualified with package name), 
> does it
> output the entire module code?

Yes

> what does this output?
>
> int foo() { return 0;}
> int foo(int i) { return i+1; }
> void foo(double d) { }
>
> foreach(i,overload; __traits(getOverloads, "foo"))
>     writeln(overload.codef);

int foo();

int foo(int i);

void foo(double d);
May 17, 2012
Re: New Traits
On 2012-05-16 23:19, John Maschmeyer wrote:

> It works for a function literal that has been assigned to a variable.
>
> Function Literal:
> int function(int) func = x => x+1;
> writeln(__traits(codeof, func));
> Outputs:
> int function(int) func = delegate pure nothrow @safe int(int x)
> {
> return x + 1;
> }
> ;

Does it work if the lambda is passed to a function:

void foo (int delegate (int x) dg)
{
    wirteln(__traits(codeof, dg);
}

foo(x => x + 1);

-- 
/Jacob Carlborg
May 17, 2012
Re: New Traits
On 2012-05-16 23:20, John Maschmeyer wrote:
> On Wednesday, 16 May 2012 at 17:08:43 UTC, Philippe Sigaud wrote:
>> If you give it a module name (qualified with package name), does it
>> output the entire module code?
>
> Yes
>

Awesome, now where is that CTFE D compiler :)


-- 
/Jacob Carlborg
May 17, 2012
Re: New Traits
On Thursday, 17 May 2012 at 10:30:26 UTC, Jacob Carlborg wrote:
> Does it work if the lambda is passed to a function:
>
> void foo (int delegate (int x) dg)
> {
>     wirteln(__traits(codeof, dg);
> }
>
> foo(x => x + 1);

Unforutnately, no.  As written the lambda is a runtime parameter, 
so there is no way the trait can get the actual function code.  
Instead, all that will print is the signature of the delegate.

If that were a template parameter, it would theoretically be 
possible to print the lambda expression, but as currently 
implemented it doesn't.
May 17, 2012
Re: New Traits
On Thu, 17 May 2012 23:20:01 +0200, John Maschmeyer wrote:

> On Thursday, 17 May 2012 at 10:30:26 UTC, Jacob Carlborg wrote:
>> Does it work if the lambda is passed to a function:
>>
>> void foo (int delegate (int x) dg)
>> {
>>     wirteln(__traits(codeof, dg);
>> }
>>
>> foo(x => x + 1);
> 
> Unforutnately, no.  As written the lambda is a runtime parameter,
> so there is no way the trait can get the actual function code. Instead,
> all that will print is the signature of the delegate.
> 
> If that were a template parameter, it would theoretically be possible to
> print the lambda expression, but as currently implemented it doesn't.

An alias parameter to a template would actually be the ideal use-case for 
what I have in mind. Essentially, it would work just like map/reduce/
filter (function passed by alias), but translate the lambda to SQL/XPath/
etc.
May 18, 2012
Re: New Traits
On Thursday, 17 May 2012 at 21:39:03 UTC, Justin Whear wrote:
> An alias parameter to a template would actually be the ideal 
> use-case for
> what I have in mind. Essentially, it would work just like 
> map/reduce/
> filter (function passed by alias), but translate the lambda to 
> SQL/XPath/
> etc.

I just pushed an update that should support this.

It can now resolve aliases and lambdas properly.  This means 
things like this will work correctly.

module pack.test;
int foo() { return 0;}
int foo(int i) { return i+1; }
void foo(double d) { }

foreach(overload; __traits(getOverloads, pack.test, "foo"))
   writeln(__traits(codeof, overload);

Also, passing lambdas via template alias parameters works.  So 
something like this should work as well.

void foo(alias dg)() {
   writeln(__traits(codeof, dg));
}

void main() {
   foo!(x=>x+1)();
}
May 18, 2012
Re: New Traits
On 2012-05-18 04:21, John Maschmeyer wrote:

> Also, passing lambdas via template alias parameters works. So something
> like this should work as well.
>
> void foo(alias dg)() {
> writeln(__traits(codeof, dg));
> }
>
> void main() {
> foo!(x=>x+1)();
> }

Very nice.

-- 
/Jacob Carlborg
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home