www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - New Traits

reply "John Maschmeyer" <jmaschme gmail.com> writes:
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 14 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
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 14 2012
prev sibling next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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.
 =C2=A0 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
parent reply "John Maschmeyer" <jmaschme gmail.com> writes:
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 15 2012
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
On Tue, May 15, 2012 at 11:09 PM, John Maschmeyer <jmaschme gmail.com> wrot=
e:
 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. =C2=A0If you just use th=
e symbol
 name, it works like other traits and returns the source code for the firs=
t
 match. =C2=A0I tried using the getOverloads trait and some alias magic, b=
ut all
 I've been able to do so far is get the prototype for overloads. =C2=A0I'm
 guessing it has something to do with using an alias instead of the actual
 symbol. =C2=A0I think we need a better way to reference an overloaded sym=
bol. 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
parent reply "John Maschmeyer" <jmaschme gmail.com> writes:
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 16 2012
parent Jacob Carlborg <doob me.com> writes:
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
prev sibling next sibling parent reply "F i L" <witte2008 gmail.com> writes:
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 15 2012
parent "Nick Sabalausky" <SeeWebsiteToContactMe semitwist.com> writes:
"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 15 2012
prev sibling parent reply Justin Whear <justin economicmodeling.com> writes:
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
next sibling parent Jacob Carlborg <doob me.com> writes:
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
prev sibling parent reply "John Maschmeyer" <jmaschme gmail.com> writes:
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; } ;
May 16 2012
parent reply Jacob Carlborg <doob me.com> writes:
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
parent reply "John Maschmeyer" <jmaschme gmail.com> writes:
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
parent reply Justin Whear <justin economicmodeling.com> writes:
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 17 2012
parent reply "John Maschmeyer" <jmaschme gmail.com> writes:
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 17 2012
parent Jacob Carlborg <doob me.com> writes:
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
May 17 2012