View mode: basic / threaded / horizontal-split · Log in · Help
May 14, 2012
New Traits
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/huyqfcoosgzfneswnrur@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
May 15, 2012
Re: New Traits
On 2012-05-15 01:13, John Maschmeyer wrote:
> 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/huyqfcoosgzfneswnrur@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

Wow, all these are awesome. Nice work.

-- 
/Jacob Carlborg
May 15, 2012
Re: New Traits
On Tue, May 15, 2012 at 1:13 AM, John Maschmeyer <jmaschme@gmail.com> wrote:
> 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.

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

Awesome!

Can you give us a simple example of what codeof produces? How does it
deal with functions overloads?
May 15, 2012
Re: New Traits
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.
May 16, 2012
Re: New Traits
On Monday, 14 May 2012 at 23:13:29 UTC, John Maschmeyer wrote:
> 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/huyqfcoosgzfneswnrur@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

This is great! Can't wait to use these in action (codeof mostly), 
hopefully it'll make it into 2.030.

:D
May 16, 2012
Re: New Traits
"F i L" <witte2008@gmail.com> wrote in message 
news:amhbhvoaaxncbyalouuv@forum.dlang.org...
>
> This is great! Can't wait to use these in action (codeof mostly), 
> hopefully it'll make it into 2.030.
>

Little too late for that. Maybe for 2.060, though. </smartass>
May 16, 2012
Re: New Traits
On Tue, 15 May 2012 01:13:27 +0200, John Maschmeyer wrote:

> 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/huyqfcoosgzfneswnrur@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

Does codeof work with lambda literals? If so, I'm envisioning a LINQ-to-
SQL-esque library.
May 16, 2012
Re: New Traits
On Tue, May 15, 2012 at 11:09 PM, John Maschmeyer <jmaschme@gmail.com> wrote:
> 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?

(examples)
> *************

Wonderful. I'm already salivating, er I mean I'm quite eager to get my
hands on it.

Jeebus, we will be able to do wonderful things with this. Code
extraction, parsing, AST modif and then code re-creation and mixin.


If you give it a module name (qualified with package name), does it
output the entire module code?


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

Don't sweat it for now. It's a bit more uncommon.

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);
May 16, 2012
Re: New Traits
On 2012-05-16 19:04, Justin Whear wrote:

> Does codeof work with lambda literals? If so, I'm envisioning a LINQ-to-
> SQL-esque library.

That would be awesome to do. Actually, if D had operator overloading 
that worked a bit more like it does in C++ that would be sufficient in 
most cases.

-- 
/Jacob Carlborg
May 16, 2012
Re: New Traits
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;
  }
  ;
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home