Re: New Traits

2012-05-17 Thread John Maschmeyer

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)();
}


Re: New Traits

2012-05-17 Thread John Maschmeyer

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.


Re: New Traits

2012-05-16 Thread John Maschmeyer

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);


Re: New Traits

2012-05-16 Thread John Maschmeyer

On Wednesday, 16 May 2012 at 17:04:32 UTC, Justin Whear wrote:
Does codeof work with lambda literals? If so, I'm envisioning a 
LINQ-to-

SQL-esque library.


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;
  }
  ;


Re: New Traits

2012-05-15 Thread John Maschmeyer

On Tuesday, 15 May 2012 at 17:18:57 UTC, Philippe Sigaud wrote:
Can you give us a simple example of what codeof produces? How 
does it

deal with functions overloads?


import std.stdio;

void bar() {
   writeln("testing");
}

struct Foo {
 public:
  int x;
  int y;
}

void main() {
   writeln("*");
   writeln(__traits(codeof, bar));
   writeln("*");
   writeln(__traits(codeof, Foo));
   writeln("*");
}

This prints:
*
void bar()
{
writeln("testing");
}

*
struct Foo
{
public
{
int x;
int y;
}
}

*

I'm not sure how well this works with overloads.  If you just use 
the symbol name, it works like other traits and returns the 
source code for the first match.  I tried using the getOverloads 
trait and some alias magic, but all I've been able to do so far 
is get the prototype for overloads.  I'm guessing it has 
something to do with using an alias instead of the actual symbol. 
 I think we need a better way to reference an overloaded symbol.


New Traits

2012-05-14 Thread John Maschmeyer

I implemented some new traits that seemed to have a lot of
interest recently.

I've implemented parameterNames, isPublic, isPrivate,
isProtected, isPackge, isExport, and codeof traits.

parameterNames lets you get access to the names of function
parameters

isPublic, isPrivate, etc let you query access modifiers

codeof was discussed recently in
http://forum.dlang.org/thread/huyqfcoosgzfneswn...@forum.dlang.org.
   It gives you access to the source code of functions, classes,
etc.

I'm pretty new to the dmd code, so could someone take a look at
them?

https://github.com/D-Programming-Language/dmd/pull/951
https://github.com/D-Programming-Language/dmd/pull/952
https://github.com/D-Programming-Language/dmd/pull/953