www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Mixin Expressions, can't evalutate string variable

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
I think this should work:

string s = "int x;";
mixin(s);

void main()
{
    
}

But I get:
testtest.d(11): Error: argument to mixin must be a string, not (s)

I get a similar error with the template example from the docs:

template GenStruct(string Name, string M1)
{
    string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
}

mixin(GenStruct!("Foo", "bar"));

void main() { }

testtest.d(27): Error: argument to mixin must be a string, not (GenStruct)


This is really weird, I swear it worked for me a couple of days ago. :s
Maybe I accidentally modified a library file, so I'll have a look. Meanwhile
can someone succesfully compile these?
Aug 05 2010
parent reply Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
Andrej Mitrovic napisał:

 I think this should work:
 
 string s = "int x;";
 mixin(s);
 
 void main()
 {
     
 }
 
 But I get:
 testtest.d(11): Error: argument to mixin must be a string, not (s)
 
 I get a similar error with the template example from the docs:
 
 template GenStruct(string Name, string M1)
 {
     string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
 }
 
 mixin(GenStruct!("Foo", "bar"));
 
 void main() { }
 
 testtest.d(27): Error: argument to mixin must be a string, not (GenStruct)
 
 
 This is really weird, I swear it worked for me a couple of days ago. :s
 Maybe I accidentally modified a library file, so I'll have a look.
 Meanwhile can someone succesfully compile these?

template GenStruct(string Name, string M1) { immutable string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }"; } It must be immutable to be seen as a template expression. Otherwise it's one mutable string declaration that happens to be named same as the enclosing template. Tomek
Aug 05 2010
next sibling parent Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
Tomek Sowiński napisał:

 Andrej Mitrovic napisał:
 
 I think this should work:
 
 string s = "int x;";
 mixin(s);
 
 void main()
 {
     
 }
 
 But I get:
 testtest.d(11): Error: argument to mixin must be a string, not (s)
 
 I get a similar error with the template example from the docs:
 
 template GenStruct(string Name, string M1)
 {
     string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
 }
 
 mixin(GenStruct!("Foo", "bar"));
 
 void main() { }
 
 testtest.d(27): Error: argument to mixin must be a string, not
 (GenStruct)
 
 
 This is really weird, I swear it worked for me a couple of days ago. :s
 Maybe I accidentally modified a library file, so I'll have a look.
 Meanwhile can someone succesfully compile these?

template GenStruct(string Name, string M1) { immutable string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }"; } It must be immutable to be seen as a template expression. Otherwise it's one mutable string declaration that happens to be named same as the enclosing template.

To avoid the WTF you may find CTFE useful (and easier on the eye): string GenStruct(string Name, string M1) { return "struct " ~ Name ~ "{ int " ~ M1 ~ "; }"; } mixin(GenStruct("Foo", "bar")); Tomek
Aug 05 2010
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,
 because this will not compile:

string input2 = "int y;";
mixin(input2);

But this will compile:

immutable string input2 = "int y;";
mixin(input2);

And this too will compile:

string returnString(string input)
{
    return input;
}

mixin(returnString("int y;"));

Tomek Sowiński Wrote:

 Andrej Mitrovic napisał:
 
 I think this should work:
 
 string s = "int x;";
 mixin(s);
 
 void main()
 {
     
 }
 
 But I get:
 testtest.d(11): Error: argument to mixin must be a string, not (s)
 
 I get a similar error with the template example from the docs:
 
 template GenStruct(string Name, string M1)
 {
     string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }";
 }
 
 mixin(GenStruct!("Foo", "bar"));
 
 void main() { }
 
 testtest.d(27): Error: argument to mixin must be a string, not (GenStruct)
 
 
 This is really weird, I swear it worked for me a couple of days ago. :s
 Maybe I accidentally modified a library file, so I'll have a look.
 Meanwhile can someone succesfully compile these?

template GenStruct(string Name, string M1) { immutable string GenStruct = "struct " ~ Name ~ "{ int " ~ M1 ~ "; }"; } It must be immutable to be seen as a template expression. Otherwise it's one mutable string declaration that happens to be named same as the enclosing template. Tomek

Aug 05 2010
next sibling parent reply Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
Andrej Mitrovic napisał:

 Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,
  because this will not compile:
 
 string input2 = "int y;";
 mixin(input2);

input2 is mutable, so theoretically there's no telling what value it holds.
 But this will compile:
 
 immutable string input2 = "int y;";
 mixin(input2);

And good.
 And this too will compile:
 
 string returnString(string input)
 {
     return input;
 }
 
 mixin(returnString("int y;"));

Because the function is *nice*, compiler can evaluate the call at compile-time (see my other post). Somewhere on the D page (Lang. Ref. > Functions ?) there's a largish explanation what's *nice*. Tomek
Aug 05 2010
parent reply Jacob Carlborg <doob me.com> writes:
On 2010-08-05 23:50, Andrej Mitrovic wrote:
 On Thu, Aug 5, 2010 at 11:29 PM, Philippe Sigaud
 <philippe.sigaud gmail.com <mailto:philippe.sigaud gmail.com>> wrote:



     On Thu, Aug 5, 2010 at 22:24, Andrej Mitrovic
     <andrej.mitrovich gmail.com <mailto:andrej.mitrovich gmail.com>> wrote:

         Thanks, Steven!

         You don't want to know what I'm up to :p.


     Yes, yes, wo do!


         I'm using some traits to find out what kind of parameters a
         function takes. I'm also using a demangler from phobos to find
         out the name of the function (But I can't find any
         straightforward way to get a name of a function without getting
         back "1D_5std_5funcName" etc, so I just take a hardcoded slice).


     You can get the name of an alias using __traits(identifier,
     aliasName), like this for example:

     template Name(alias a)
     {
        enum string Name = __traits(identifier, a);
     }

     int foo(int i) { return 0;}

     import std.stdio;
     void main()
     {
        writeln(Name!foo); // "foo", not "a".
     }


     As for demangling, how do you do to get mangled names in the first
     place?


 I was using mangledName!() from std.straits. __traits works prefectly,
 Thanks!

You know there is a .mangleof property for all symbols.
         Anyway, I have a template function which takes as it's
         parameters a function (aliased to func), and some arguments
         (right now just one in my hardcoded code). It then creates a
         string which can be compiled via the mixin. It constructs the
         string by checking the parameter types of func, and for any ref
         or out parameter it detects it appends something like this to a
         string:
         "long var1; long var2; long var3; ".


     How can you know if a parameter is ref or out?
     I remember seeing something about it in Phobos svn, are you using it?


 Not svn, it's in 2.047 (maybe in earlier ones as well) in std.traits:

 ParameterTypeTuple!(alias) - for the types
 ParameterStorageClassTuple!(alias) - for the storage class

         So anyway, at the calling site I have this:

         mixin(unpack!(getTimes)(r"C:\\cookies"));

         getTimes() is a Phobos function with the signature:
         getTimes(in char[] name, out d_time ftc, out d_time fta, out
         d_time ftm)

         And now I automatically have var1, var2, and var3 at my disposal.


     That's fun :)


         It was just an exercise for fun but it's cool that things like
         this are possible in D. It would be nice if I could get the
         actual names of the parameters the function takes + the clear
         name of the function itself, that way I'd actually get back
         variables "ftc, fta, ftm" back)


     I'm pretty sure I saw some code to do this. But maybe that was a D1
     thing, using mangled names, too.
     in dsource/scrapple?

     Philippe


 Dunno, I haven't been using D1 really (actually I tried it some years
 ago with Tango but it left me wanting more so I never stuck around
 much). But D2 is super-fun.

-- /Jacob Carlborg
Aug 06 2010
next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Friday, August 06, 2010 08:08:03 Andrej Mitrovic wrote:
 I've already tried that. But .mangleof on an aliased symbol just returns
 "alias" as a string.

That sounds like a bug report in the making. - Jonathan M Davis
Aug 06 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5b05f5de17b048d2d6d25
Content-Type: text/plain; charset=ISO-8859-1

Andrej:


 It was just an exercise for fun but it's cool that things like this are
 possible in D. It would be nice if I could get the actual names of the
 parameters the function takes + the clear name of the function itself, that
 way I'd actually get back variables "ftc, fta, ftm" back)

There, found it again, while answering another thread: int foo(int i, double d) { return 0;} writeln(typeof(&foo).stringof); // "int function(int i, double d)" <-- Look Ma, arguments names! But it's a quirk of .stringof, I'm not sure it's a good idea to rely on it too much. from there, using compile-time search in a string, you can extract the arguments (those are between ( and ) ) -> "int i, double d" and from there, extracting i and d. I don't what will happen for overloaded functions, methods names, constructors, ... Philippe --001636c5b05f5de17b048d2d6d25 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Andrej:<br><div class=3D"im"><div>=A0</div><blockquote class=3D"gmail_quote= " style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, = 204); padding-left: 1ex;"> <br>It was just an exercise for fun but it&#39;s cool that things like this= =20 are possible in D. It would be nice if I could get the actual names of=20 the parameters the function takes + the clear name of the function=20 itself, that way I&#39;d actually get back variables &quot;ftc, fta, ftm&qu= ot; back)</blockquote> <div><br></div></div>There, found it again, while answering another thread:= <br><br>int foo(int i, double d) { return 0;}<br><br>writeln(typeof(&amp;fo= o).stringof); // &quot;int function(int i, double d)&quot; &lt;-- Look Ma, = arguments names!<br> <br>But it&#39;s a quirk of .stringof, I&#39;m not sure it&#39;s a good ide= a to rely on it too much.<br>from there, using compile-time search in a str= ing, you can extract the arguments (those are between ( and ) )<br>-&gt; &q= uot;int i, double d&quot;<br> and from there, extracting i and d.<br><br>I don&#39;t what will happen for= overloaded functions, methods names, constructors, ...<br><br><br>Philippe= <br><br> --001636c5b05f5de17b048d2d6d25--
Aug 06 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--e0cb4e88767366849e048d2ef60b
Content-Type: text/plain; charset=ISO-8859-1

Slightly OT: I've noticed you're often missing the word "know" in your posts
(e.g. "I don't what", that should be "I don't know what"). Is something
filtering your posts? :)

And yeah, I've noticed your other thread with the argument names. With a
little bit of regex I could easily extract the variable names. I think this
template could be useful in cases when you just want to try out a function
which happens to writes some state in the parameters that are passed to it
(out/ref params), without having to inspect the function signature and
declare the proper variable types. Unfortunately there's no way to pass auto
variables as parameters, but that's more of a Python territory, I guess.

On the other hand, the template introduces new identifiers silently into the
calling site (you can't see it in the code), so it's not all that practical
I guess, not to mention a little dangerous. :p

On Fri, Aug 6, 2010 at 10:22 PM, Philippe Sigaud
<philippe.sigaud gmail.com>wrote:

 Andrej:



 It was just an exercise for fun but it's cool that things like this are
 possible in D. It would be nice if I could get the actual names of the
 parameters the function takes + the clear name of the function itself, that
 way I'd actually get back variables "ftc, fta, ftm" back)

There, found it again, while answering another thread: int foo(int i, double d) { return 0;} writeln(typeof(&foo).stringof); // "int function(int i, double d)" <-- Look Ma, arguments names! But it's a quirk of .stringof, I'm not sure it's a good idea to rely on it too much. from there, using compile-time search in a string, you can extract the arguments (those are between ( and ) ) -> "int i, double d" and from there, extracting i and d. I don't what will happen for overloaded functions, methods names, constructors, ... Philippe

--e0cb4e88767366849e048d2ef60b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Slightly OT: I&#39;ve noticed you&#39;re often missing the word &quot;know&= quot; in your posts (e.g. &quot;I don&#39;t what&quot;, that should be &quo= t;I don&#39;t know what&quot;). Is something filtering your posts? :)<br> <br>And yeah, I&#39;ve noticed your other thread with the argument names. W= ith a little bit of regex I could easily extract the variable names. I thin= k this template could be useful in cases when you just want to try out a fu= nction which happens to writes some state in the parameters that are passed= to it (out/ref params), without having to inspect the function signature a= nd declare the proper variable types. Unfortunately there&#39;s no way to p= ass auto variables as parameters, but that&#39;s more of a Python territory= , I guess.<br> <br>On the other hand, the template introduces new identifiers silently int= o the calling site (you can&#39;t see it in the code), so it&#39;s not all = that practical I guess, not to mention a little dangerous. :p<br><br><div c= lass=3D"gmail_quote"> On Fri, Aug 6, 2010 at 10:22 PM, Philippe Sigaud <span dir=3D"ltr">&lt;<a h= ref=3D"mailto:philippe.sigaud gmail.com">philippe.sigaud gmail.com</a>&gt;<= /span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt= 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Andrej:<div class=3D"im"><br><div><div>=A0</div><blockquote class=3D"gmail_= quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, = 204, 204); padding-left: 1ex;"> <br>It was just an exercise for fun but it&#39;s cool that things like this= =20 are possible in D. It would be nice if I could get the actual names of=20 the parameters the function takes + the clear name of the function=20 itself, that way I&#39;d actually get back variables &quot;ftc, fta, ftm&qu= ot; back)</blockquote> <div><br></div></div></div>There, found it again, while answering another t= hread:<br><br>int foo(int i, double d) { return 0;}<br><br>writeln(typeof(&= amp;foo).stringof); // &quot;int function(int i, double d)&quot; &lt;-- Loo= k Ma, arguments names!<br> <br>But it&#39;s a quirk of .stringof, I&#39;m not sure it&#39;s a good ide= a to rely on it too much.<br>from there, using compile-time search in a str= ing, you can extract the arguments (those are between ( and ) )<br>-&gt; &q= uot;int i, double d&quot;<br> and from there, extracting i and d.<br><br>I don&#39;t what will happen for= overloaded functions, methods names, constructors, ...<br><font color=3D"#= 888888"><br><br>Philippe<br><br> </font></blockquote></div><br> --e0cb4e88767366849e048d2ef60b--
Aug 06 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--e0cb4e887ee9ee9de3048d3172eb
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Aug 6, 2010 at 8:40 PM, Jonathan M Davis <jmdavisprog gmail.com>wrote:

 On Friday, August 06, 2010 08:08:03 Andrej Mitrovic wrote:
 I've already tried that. But .mangleof on an aliased symbol just returns
 "alias" as a string.

That sounds like a bug report in the making. - Jonathan M Davis

No I was wrong, it won't allow me to use .mangleof on an alias of a function inside a template because that automatically calls the function (and that means I have to pass valid arguments to it). The mangledName!() template works just fine though. But I don't need mangled names, I was only using it as a workaround. --e0cb4e887ee9ee9de3048d3172eb Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Fri, Aug 6, 2010 at 8:40 PM, Jonathan= M Davis <span dir=3D"ltr">&lt;<a href=3D"mailto:jmdavisprog gmail.com">jmd= avisprog gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quot= e" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204,= 204); padding-left: 1ex;"> <div class=3D"im">On Friday, August 06, 2010 08:08:03 Andrej Mitrovic wrote= :<br> &gt; I&#39;ve already tried that. But .mangleof on an aliased symbol just r= eturns<br> &gt; &quot;alias&quot; as a string.<br> <br> </div>That sounds like a bug report in the making.<br> <font color=3D"#888888"><br> - Jonathan M Davis<br> </font></blockquote></div><br>No I was wrong, it won&#39;t allow me to use = .mangleof on an alias of a function inside a template because that automati= cally calls the function (and that means I have to pass valid arguments to = it). The mangledName!() template works just fine though. But I don&#39;t ne= ed mangled names, I was only using it as a workaround.<br> --e0cb4e887ee9ee9de3048d3172eb--
Aug 06 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--0016e647586666bc99048d31a590
Content-Type: text/plain; charset=ISO-8859-1

Actually I better report this, it might lead to unexpected behavior
otherwise. See, this will compile:

import std.stdio;

auto template_func(alias func, T...)(T args)
{
    return func.mangleof;
}

void test() {}

unittest
{
    writeln(template_func!(test, int)(1));
}

void main() { }


And this won't:

import std.stdio;

auto template_func(alias func, T...)(T args)
{
    return func.mangleof;
}

void test(int x) {}

unittest
{
    writeln(template_func!(test, int)(1));
}

void main() { }

(I've added a parameter to test()). The problem is the first code actually
called func, and succeeded silently since it takes no parameters. I don't
think this is an expected behavior?

On Sat, Aug 7, 2010 at 3:10 AM, Andrej Mitrovic
<andrej.mitrovich gmail.com>wrote:

 On Fri, Aug 6, 2010 at 8:40 PM, Jonathan M Davis <jmdavisprog gmail.com>wrote:

 On Friday, August 06, 2010 08:08:03 Andrej Mitrovic wrote:
 I've already tried that. But .mangleof on an aliased symbol just returns
 "alias" as a string.

That sounds like a bug report in the making. - Jonathan M Davis

No I was wrong, it won't allow me to use .mangleof on an alias of a function inside a template because that automatically calls the function (and that means I have to pass valid arguments to it). The mangledName!() template works just fine though. But I don't need mangled names, I was only using it as a workaround.

--0016e647586666bc99048d31a590 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Actually I better report this, it might lead to unexpected behavior otherwi= se. See, this will compile:<br><br>import std.stdio;<br><br>auto template_f= unc(alias func, T...)(T args)<br>{<br>=A0=A0=A0 return func.mangleof;<br>}<= br> <br>void test() {}<br><br>unittest<br>{=A0=A0=A0 <br>=A0=A0=A0 writeln(temp= late_func!(test, int)(1));<br>}<br><br>void main() { }<br><br><br>And this = won&#39;t:<br><br>import std.stdio;<br><br>auto template_func(alias func, T= ...)(T args)<br> {<br>=A0=A0=A0 return func.mangleof;<br>}<br><br>void test(int x) {}<br><br=
unittest<br>{=A0=A0=A0 <br>=A0=A0=A0 writeln(template_func!(test, int)(1))=

. The problem is the first code actually called func, and succeeded silentl= y since it takes no parameters. I don&#39;t think this is an expected behav= ior?<br> <br><div class=3D"gmail_quote">On Sat, Aug 7, 2010 at 3:10 AM, Andrej Mitro= vic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gmail.com">and= rej.mitrovich gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail= _quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204,= 204, 204); padding-left: 1ex;"> <div><div></div><div class=3D"h5"><br><br><div class=3D"gmail_quote">On Fri= , Aug 6, 2010 at 8:40 PM, Jonathan M Davis <span dir=3D"ltr">&lt;<a href=3D= "mailto:jmdavisprog gmail.com" target=3D"_blank">jmdavisprog gmail.com</a>&= gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <div>On Friday, August 06, 2010 08:08:03 Andrej Mitrovic wrote:<br> &gt; I&#39;ve already tried that. But .mangleof on an aliased symbol just r= eturns<br> &gt; &quot;alias&quot; as a string.<br> <br> </div>That sounds like a bug report in the making.<br> <font color=3D"#888888"><br> - Jonathan M Davis<br> </font></blockquote></div><br></div></div>No I was wrong, it won&#39;t allo= w me to use .mangleof on an alias of a function inside a template because t= hat automatically calls the function (and that means I have to pass valid a= rguments to it). The mangledName!() template works just fine though. But I = don&#39;t need mangled names, I was only using it as a workaround.<br> </blockquote></div><br> --0016e647586666bc99048d31a590--
Aug 06 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--0016e647185ed31808048d323293
Content-Type: text/plain; charset=ISO-8859-1

Damn regexe(s|n).

I almost never need them, but when I do, I have to spend an hour trying to
remember the syntax (+ I'm really used to doing regexes in Python and then
there's the whole "regex objects in lang x work different than in lang y"
thing). But I digress..

I think a better way is to pass strings which will be used to name the
variables to be constructed and initialized (+ return an error when there's
too few). And then at least you'll know there are some new local variables
at your disposal by looking at the call. That way we solve both problems.

On Sat, Aug 7, 2010 at 12:12 AM, Andrej Mitrovic <andrej.mitrovich gmail.com
 wrote:

 Slightly OT: I've noticed you're often missing the word "know" in your
 posts (e.g. "I don't what", that should be "I don't know what"). Is
 something filtering your posts? :)

 And yeah, I've noticed your other thread with the argument names. With a
 little bit of regex I could easily extract the variable names. I think this
 template could be useful in cases when you just want to try out a function
 which happens to writes some state in the parameters that are passed to it
 (out/ref params), without having to inspect the function signature and
 declare the proper variable types. Unfortunately there's no way to pass auto
 variables as parameters, but that's more of a Python territory, I guess.

 On the other hand, the template introduces new identifiers silently into
 the calling site (you can't see it in the code), so it's not all that
 practical I guess, not to mention a little dangerous. :p


 On Fri, Aug 6, 2010 at 10:22 PM, Philippe Sigaud <
 philippe.sigaud gmail.com> wrote:

 Andrej:



 It was just an exercise for fun but it's cool that things like this are
 possible in D. It would be nice if I could get the actual names of the
 parameters the function takes + the clear name of the function itself, that
 way I'd actually get back variables "ftc, fta, ftm" back)

There, found it again, while answering another thread: int foo(int i, double d) { return 0;} writeln(typeof(&foo).stringof); // "int function(int i, double d)" <-- Look Ma, arguments names! But it's a quirk of .stringof, I'm not sure it's a good idea to rely on it too much. from there, using compile-time search in a string, you can extract the arguments (those are between ( and ) ) -> "int i, double d" and from there, extracting i and d. I don't what will happen for overloaded functions, methods names, constructors, ... Philippe


--0016e647185ed31808048d323293 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Damn regexe(s|n).<br><br>I almost never need them, but when I do, I have to= spend an hour trying to remember the syntax (+ I&#39;m really used to doin= g regexes in Python and then there&#39;s the whole &quot;regex objects in l= ang x work different than in lang y&quot; thing). But I digress..<br> <br>I think a better way is to pass strings which will be used to name the = variables to be constructed and initialized (+ return an error when there&#= 39;s too few). And then at least you&#39;ll know there are some new local v= ariables at your disposal by looking at the call. That way we solve both pr= oblems.<br> <br><div class=3D"gmail_quote">On Sat, Aug 7, 2010 at 12:12 AM, Andrej Mitr= ovic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gmail.com">an= drej.mitrovich gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmai= l_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204= , 204, 204); padding-left: 1ex;"> Slightly OT: I&#39;ve noticed you&#39;re often missing the word &quot;know&= quot; in your posts (e.g. &quot;I don&#39;t what&quot;, that should be &quo= t;I don&#39;t know what&quot;). Is something filtering your posts? :)<br> <br>And yeah, I&#39;ve noticed your other thread with the argument names. W= ith a little bit of regex I could easily extract the variable names. I thin= k this template could be useful in cases when you just want to try out a fu= nction which happens to writes some state in the parameters that are passed= to it (out/ref params), without having to inspect the function signature a= nd declare the proper variable types. Unfortunately there&#39;s no way to p= ass auto variables as parameters, but that&#39;s more of a Python territory= , I guess.<br> <br>On the other hand, the template introduces new identifiers silently int= o the calling site (you can&#39;t see it in the code), so it&#39;s not all = that practical I guess, not to mention a little dangerous. :p<div><div> </div><div class=3D"h5"><br><br><div class=3D"gmail_quote"> On Fri, Aug 6, 2010 at 10:22 PM, Philippe Sigaud <span dir=3D"ltr">&lt;<a h= ref=3D"mailto:philippe.sigaud gmail.com" target=3D"_blank">philippe.sigaud = gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> Andrej:<div><br><div><div>=A0</div><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> <br>It was just an exercise for fun but it&#39;s cool that things like this= =20 are possible in D. It would be nice if I could get the actual names of=20 the parameters the function takes + the clear name of the function=20 itself, that way I&#39;d actually get back variables &quot;ftc, fta, ftm&qu= ot; back)</blockquote> <div><br></div></div></div>There, found it again, while answering another t= hread:<br><br>int foo(int i, double d) { return 0;}<br><br>writeln(typeof(&= amp;foo).stringof); // &quot;int function(int i, double d)&quot; &lt;-- Loo= k Ma, arguments names!<br> <br>But it&#39;s a quirk of .stringof, I&#39;m not sure it&#39;s a good ide= a to rely on it too much.<br>from there, using compile-time search in a str= ing, you can extract the arguments (those are between ( and ) )<br>-&gt; &q= uot;int i, double d&quot;<br> and from there, extracting i and d.<br><br>I don&#39;t what will happen for= overloaded functions, methods names, constructors, ...<br><font color=3D"#= 888888"><br><br>Philippe<br><br> </font></blockquote></div><br> </div></div></blockquote></div><br> --0016e647185ed31808048d323293--
Aug 06 2010
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--0016e6d274b8eca05e048d40b05c
Content-Type: text/plain; charset=ISO-8859-1

You know, I just had an idea. It would be really cool to have some sort of
generalized template function which can test other functions in various
ways. You could pass strings as options, where you might choose the type of
testing being done, such as verification tests or performance tests.. Or if
a function is designed to write a file on disk, maybe you'd want to have an
option for the maximum number of tests to run on that function (you don't
want to be left with 10000 files written in some temp directory
somewhere..), or a test to see if a function will fail with invalid input,
etc.

Well it's just an idea, I better take a look at the existing test frameworks
and see how it's done there.

On Sat, Aug 7, 2010 at 4:04 AM, Andrej Mitrovic
<andrej.mitrovich gmail.com>wrote:

 Damn regexe(s|n).

 I almost never need them, but when I do, I have to spend an hour trying to
 remember the syntax (+ I'm really used to doing regexes in Python and then
 there's the whole "regex objects in lang x work different than in lang y"
 thing). But I digress..

 I think a better way is to pass strings which will be used to name the
 variables to be constructed and initialized (+ return an error when there's
 too few). And then at least you'll know there are some new local variables
 at your disposal by looking at the call. That way we solve both problems.


 On Sat, Aug 7, 2010 at 12:12 AM, Andrej Mitrovic <
 andrej.mitrovich gmail.com> wrote:

 Slightly OT: I've noticed you're often missing the word "know" in your
 posts (e.g. "I don't what", that should be "I don't know what"). Is
 something filtering your posts? :)

 And yeah, I've noticed your other thread with the argument names. With a
 little bit of regex I could easily extract the variable names. I think this
 template could be useful in cases when you just want to try out a function
 which happens to writes some state in the parameters that are passed to it
 (out/ref params), without having to inspect the function signature and
 declare the proper variable types. Unfortunately there's no way to pass auto
 variables as parameters, but that's more of a Python territory, I guess.

 On the other hand, the template introduces new identifiers silently into
 the calling site (you can't see it in the code), so it's not all that
 practical I guess, not to mention a little dangerous. :p


 On Fri, Aug 6, 2010 at 10:22 PM, Philippe Sigaud <
 philippe.sigaud gmail.com> wrote:

 Andrej:



 It was just an exercise for fun but it's cool that things like this are
 possible in D. It would be nice if I could get the actual names of the
 parameters the function takes + the clear name of the function itself, that
 way I'd actually get back variables "ftc, fta, ftm" back)

There, found it again, while answering another thread: int foo(int i, double d) { return 0;} writeln(typeof(&foo).stringof); // "int function(int i, double d)" <-- Look Ma, arguments names! But it's a quirk of .stringof, I'm not sure it's a good idea to rely on it too much. from there, using compile-time search in a string, you can extract the arguments (those are between ( and ) ) -> "int i, double d" and from there, extracting i and d. I don't what will happen for overloaded functions, methods names, constructors, ... Philippe



--0016e6d274b8eca05e048d40b05c Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable You know, I just had an idea. It would be really cool to have some sort of = generalized template function which can test other functions in various way= s. You could pass strings as options, where you might choose the type of te= sting being done, such as verification tests or performance tests.. Or if a= function is designed to write a file on disk, maybe you&#39;d want to have= an option for the maximum number of tests to run on that function (you don= &#39;t want to be left with 10000 files written in some temp directory some= where..), or a test to see if a function will fail with invalid input, etc.= <br> <br>Well it&#39;s just an idea, I better take a look at the existing test f= rameworks and see how it&#39;s done there.<br><br><div class=3D"gmail_quote= ">On Sat, Aug 7, 2010 at 4:04 AM, Andrej Mitrovic <span dir=3D"ltr">&lt;<a = href=3D"mailto:andrej.mitrovich gmail.com">andrej.mitrovich gmail.com</a>&g= t;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Damn regexe(s|n).= <br><br>I almost never need them, but when I do, I have to spend an hour tr= ying to remember the syntax (+ I&#39;m really used to doing regexes in Pyth= on and then there&#39;s the whole &quot;regex objects in lang x work differ= ent than in lang y&quot; thing). But I digress..<br> <br>I think a better way is to pass strings which will be used to name the = variables to be constructed and initialized (+ return an error when there&#= 39;s too few). And then at least you&#39;ll know there are some new local v= ariables at your disposal by looking at the call. That way we solve both pr= oblems.<div> <div></div><div class=3D"h5"><br> <br><div class=3D"gmail_quote">On Sat, Aug 7, 2010 at 12:12 AM, Andrej Mitr= ovic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gmail.com" ta= rget=3D"_blank">andrej.mitrovich gmail.com</a>&gt;</span> wrote:<br><blockq= uote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left:= 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Slightly OT: I&#39;ve noticed you&#39;re often missing the word &quot;know&= quot; in your posts (e.g. &quot;I don&#39;t what&quot;, that should be &quo= t;I don&#39;t know what&quot;). Is something filtering your posts? :)<br> <br>And yeah, I&#39;ve noticed your other thread with the argument names. W= ith a little bit of regex I could easily extract the variable names. I thin= k this template could be useful in cases when you just want to try out a fu= nction which happens to writes some state in the parameters that are passed= to it (out/ref params), without having to inspect the function signature a= nd declare the proper variable types. Unfortunately there&#39;s no way to p= ass auto variables as parameters, but that&#39;s more of a Python territory= , I guess.<br> <br>On the other hand, the template introduces new identifiers silently int= o the calling site (you can&#39;t see it in the code), so it&#39;s not all = that practical I guess, not to mention a little dangerous. :p<div><div> </div><div><br><br><div class=3D"gmail_quote"> On Fri, Aug 6, 2010 at 10:22 PM, Philippe Sigaud <span dir=3D"ltr">&lt;<a h= ref=3D"mailto:philippe.sigaud gmail.com" target=3D"_blank">philippe.sigaud = gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> Andrej:<div><br><div><div>=A0</div><blockquote class=3D"gmail_quote" style= =3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); p= adding-left: 1ex;"> <br>It was just an exercise for fun but it&#39;s cool that things like this= =20 are possible in D. It would be nice if I could get the actual names of=20 the parameters the function takes + the clear name of the function=20 itself, that way I&#39;d actually get back variables &quot;ftc, fta, ftm&qu= ot; back)</blockquote> <div><br></div></div></div>There, found it again, while answering another t= hread:<br><br>int foo(int i, double d) { return 0;}<br><br>writeln(typeof(&= amp;foo).stringof); // &quot;int function(int i, double d)&quot; &lt;-- Loo= k Ma, arguments names!<br> <br>But it&#39;s a quirk of .stringof, I&#39;m not sure it&#39;s a good ide= a to rely on it too much.<br>from there, using compile-time search in a str= ing, you can extract the arguments (those are between ( and ) )<br>-&gt; &q= uot;int i, double d&quot;<br> and from there, extracting i and d.<br><br>I don&#39;t what will happen for= overloaded functions, methods names, constructors, ...<br><font color=3D"#= 888888"><br><br>Philippe<br><br> </font></blockquote></div><br> </div></div></blockquote></div><br> </div></div></blockquote></div><br> --0016e6d274b8eca05e048d40b05c--
Aug 07 2010
parent "Nick Sabalausky" <a a.a> writes:
"Andrej Mitrovic" <andrej.mitrovich gmail.com> wrote in message 
news:mailman.175.1281208901.13841.digitalmars-d puremagic.com...
 You know, I just had an idea. It would be really cool to have some sort of
 generalized template function which can test other functions in various
 ways. You could pass strings as options, where you might choose the type 
 of
 testing being done, such as verification tests or performance tests.. Or 
 if
 a function is designed to write a file on disk, maybe you'd want to have 
 an
 option for the maximum number of tests to run on that function (you don't
 want to be left with 10000 files written in some temp directory
 somewhere..), or a test to see if a function will fail with invalid input,
 etc.

 Well it's just an idea, I better take a look at the existing test 
 frameworks
 and see how it's done there.

Probably not quite as fancy as what you're talking about, but my SemiTwist D Tools library has a module that includes something that's similar to assert but: - Allows to you verify that a particular statement throws a particular type of exception. - Properly reports if an expression throws (and you didn't expect it to). - Doesn't abort the program on failure. - Reports "expected" expression and "actual" value. http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/apps/tests/deferAssertTest/main.d http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/deferAssert.d It does need some clean-up and improvements, but it at least works. (Although, trunk is currently in the process of switching from D1/Tango to D2/Phobos, so the latest revisions might be broken and it definitely doesn't take advantage of D2-specific features yet. But the version included with Goldie 0.3 does work fine on D1/Tango though).
Aug 07 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--005045014132909c95048d41327f
Content-Type: text/plain; charset=ISO-8859-1

Cool stuff! I'll have a look later. Thanks.

On Sat, Aug 7, 2010 at 9:53 PM, Nick Sabalausky <a a.a> wrote:

 "Andrej Mitrovic" <andrej.mitrovich gmail.com> wrote in message
 news:mailman.175.1281208901.13841.digitalmars-d puremagic.com...
 You know, I just had an idea. It would be really cool to have some sort

 generalized template function which can test other functions in various
 ways. You could pass strings as options, where you might choose the type
 of
 testing being done, such as verification tests or performance tests.. Or
 if
 a function is designed to write a file on disk, maybe you'd want to have
 an
 option for the maximum number of tests to run on that function (you don't
 want to be left with 10000 files written in some temp directory
 somewhere..), or a test to see if a function will fail with invalid

 etc.

 Well it's just an idea, I better take a look at the existing test
 frameworks
 and see how it's done there.

Probably not quite as fancy as what you're talking about, but my SemiTwist D Tools library has a module that includes something that's similar to assert but: - Allows to you verify that a particular statement throws a particular type of exception. - Properly reports if an expression throws (and you didn't expect it to). - Doesn't abort the program on failure. - Reports "expected" expression and "actual" value. http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/apps/tests/deferAssertTest/main.d http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/deferAssert.d It does need some clean-up and improvements, but it at least works. (Although, trunk is currently in the process of switching from D1/Tango to D2/Phobos, so the latest revisions might be broken and it definitely doesn't take advantage of D2-specific features yet. But the version included with Goldie 0.3 does work fine on D1/Tango though).

--005045014132909c95048d41327f Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Cool stuff! I&#39;ll have a look later. Thanks.<br><br><div class=3D"gmail_= quote">On Sat, Aug 7, 2010 at 9:53 PM, Nick Sabalausky <span dir=3D"ltr">&l= t;a a.a&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"mar= gin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-= left: 1ex;"> &quot;Andrej Mitrovic&quot; &lt;<a href=3D"mailto:andrej.mitrovich gmail.co= m">andrej.mitrovich gmail.com</a>&gt; wrote in message<br> news:mailman.175.1281208901.13841.digitalmars-d puremagic.com...<br> <div class=3D"im">&gt; You know, I just had an idea. It would be really coo= l to have some sort of<br> &gt; generalized template function which can test other functions in variou= s<br> &gt; ways. You could pass strings as options, where you might choose the ty= pe<br> &gt; of<br> &gt; testing being done, such as verification tests or performance tests.. = Or<br> &gt; if<br> &gt; a function is designed to write a file on disk, maybe you&#39;d want t= o have<br> &gt; an<br> &gt; option for the maximum number of tests to run on that function (you do= n&#39;t<br> &gt; want to be left with 10000 files written in some temp directory<br> &gt; somewhere..), or a test to see if a function will fail with invalid in= put,<br> &gt; etc.<br> &gt;<br> &gt; Well it&#39;s just an idea, I better take a look at the existing test<= br> &gt; frameworks<br> &gt; and see how it&#39;s done there.<br> &gt;<br> <br> </div>Probably not quite as fancy as what you&#39;re talking about, but my = SemiTwist D<br> Tools library has a module that includes something that&#39;s similar to as= sert<br> but:<br> <br> - Allows to you verify that a particular statement throws a particular type= <br> of exception.<br> - Properly reports if an expression throws (and you didn&#39;t expect it to= ).<br> - Doesn&#39;t abort the program on failure.<br> - Reports &quot;expected&quot; expression and &quot;actual&quot; value.<br> <br> <a href=3D"http://www.dsource.org/projects/semitwist/browser/trunk/src/semi= twist/apps/tests/deferAssertTest/main.d" target=3D"_blank">http://www.dsour= ce.org/projects/semitwist/browser/trunk/src/semitwist/apps/tests/deferAsser= tTest/main.d</a><br> <a href=3D"http://www.dsource.org/projects/semitwist/browser/trunk/src/semi= twist/util/deferAssert.d" target=3D"_blank">http://www.dsource.org/projects= /semitwist/browser/trunk/src/semitwist/util/deferAssert.d</a><br> <br> It does need some clean-up and improvements, but it at least works.<br> (Although, trunk is currently in the process of switching from D1/Tango to<= br> D2/Phobos, so the latest revisions might be broken and it definitely doesn&= #39;t<br> take advantage of D2-specific features yet. But the version included with<b= r> Goldie 0.3 does work fine on D1/Tango though).<br> <br> <br> </blockquote></div><br> --005045014132909c95048d41327f--
Aug 07 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--e0cb4e8878274d9c9c048d416dbd
Content-Type: text/plain; charset=ISO-8859-1

Anyway, I've updated autoCall(), so now you can pass arguments like usual
but also specify the names of any ref/out variables that will be created. It
will also check to make sure the number of arguments matches for the call to
the function. Of course, arguments have to be evaluable at compile time (You
can't just pass a string, it has to be immutable). Here's the code:

import std.file;        // getTimes()
import std.conv;        // to!string()
import std.traits;      // Parameter types, storage, introspection

import std.stdio;

// Notes:
//  Currently it only works with simple built-in types:
//    Integrals, strings, but not objects.
//
//  It will throw a compiler error and a message if there are unmatched
//  number of arguments for the call to func().
//  Arguments have to be evaluable at compile time.
auto autoCall(alias func, T...)(T args)
{
    alias ParameterStorageClass STC;                // storage class enum
    alias ParameterTypeTuple!(func) paramTypes;          // types of
parameters
    alias ParameterStorageClassTuple!(func) storageTypes; // storage class
of parameters


    static if (paramTypes.length != args.length)
    {
        pragma(msg, "autoCall: Template Error: Unmatched number of arguments
for call to " ~ __traits(identifier, func) ~ "()");
        static assert(0);
    }

    string declString, callString;

    // eg: callString = "funcName("
    callString = __traits(identifier, func) ~ "(";

    foreach (int i, storageType; storageTypes)
    {
        if (storageType == STC.OUT || storageType == STC.REF)
        {
            // eg: declString ~= "int newParamName; "
            declString ~= paramTypes[i].stringof ~ " " ~ args[i] ~ "; ";

            // eg: callString = "funcName(" ~ "newParamName,"
            callString ~= " " ~ args[i] ~ ",";
        }
        else
        {
            // Fetch the value of args or args[i] and expand it to
callString.
            // We cannot use the actual names of the arguments since that
would
            // not work with literals (unless we added tests and more
branching)

            // need to use static if here due to array bounds checking
            static if (args.length == 1)
            {
                // eg: callString = "funcName(args, newParamName,"
                callString ~= '"' ~ to!string(args) ~ '"' ~ ",";
            }
            else
            {
                // eg: callString = "funcName(args[i], newParamName,"
                callString ~= '"' ~ to!string(args[i]) ~ '"' ~ ",";
            }
        }
    }

    // remove the extra comma and close the call string
    callString = callString[0 .. $ - 1] ~ ");";

    // return joined strings ready to be used with mixin()
    return declString ~= callString;
}

void testMe(string x, ref int z, string y)
{
    writeln(x, y);
    z = 5;
}

unittest
{
    immutable string var1 = "foo";
    immutable string var2 = "bar";

    mixin(autoCall!(testMe)(var1, "newvar", var2));
    assert(newvar == 5);
}

void main() { }

Fun stuff, right? :p

On Sat, Aug 7, 2010 at 9:57 PM, Andrej Mitrovic
<andrej.mitrovich gmail.com>wrote:

 Cool stuff! I'll have a look later. Thanks.


 On Sat, Aug 7, 2010 at 9:53 PM, Nick Sabalausky <a a.a> wrote:

 "Andrej Mitrovic" <andrej.mitrovich gmail.com> wrote in message
 news:mailman.175.1281208901.13841.digitalmars-d puremagic.com...
 You know, I just had an idea. It would be really cool to have some sort

 generalized template function which can test other functions in various
 ways. You could pass strings as options, where you might choose the type
 of
 testing being done, such as verification tests or performance tests.. Or
 if
 a function is designed to write a file on disk, maybe you'd want to have
 an
 option for the maximum number of tests to run on that function (you

 want to be left with 10000 files written in some temp directory
 somewhere..), or a test to see if a function will fail with invalid

 etc.

 Well it's just an idea, I better take a look at the existing test
 frameworks
 and see how it's done there.

Probably not quite as fancy as what you're talking about, but my SemiTwist D Tools library has a module that includes something that's similar to assert but: - Allows to you verify that a particular statement throws a particular type of exception. - Properly reports if an expression throws (and you didn't expect it to). - Doesn't abort the program on failure. - Reports "expected" expression and "actual" value. http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/apps/tests/deferAssertTest/main.d http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/deferAssert.d It does need some clean-up and improvements, but it at least works. (Although, trunk is currently in the process of switching from D1/Tango to D2/Phobos, so the latest revisions might be broken and it definitely doesn't take advantage of D2-specific features yet. But the version included with Goldie 0.3 does work fine on D1/Tango though).


--e0cb4e8878274d9c9c048d416dbd Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Anyway, I&#39;ve updated autoCall(), so now you can pass arguments like usu= al but also specify the names of any ref/out variables that will be created= . It will also check to make sure the number of arguments matches for the c= all to the function. Of course, arguments have to be evaluable at compile t= ime (You can&#39;t just pass a string, it has to be immutable). Here&#39;s = the code:<br> <br>import std.file;=A0=A0=A0=A0=A0=A0=A0 // getTimes()<br>import std.conv;= =A0=A0=A0=A0=A0=A0=A0 // to!string()<br>import std.traits;=A0=A0=A0=A0=A0 /= / Parameter types, storage, introspection<br><br>import std.stdio;<br><br>/= / Notes:<br>//=A0 Currently it only works with simple built-in types:<br> //=A0=A0=A0 Integrals, strings, but not objects.<br>//<br>//=A0 It will thr= ow a compiler error and a message if there are unmatched<br>//=A0 number of= arguments for the call to func().<br>//=A0 Arguments have to be evaluable = at compile time.<br> auto autoCall(alias func, T...)(T args)<br>{<br>=A0=A0=A0 alias ParameterSt= orageClass STC;=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // storage cla= ss enum<br>=A0=A0=A0 alias ParameterTypeTuple!(func) paramTypes;=A0=A0=A0= =A0=A0=A0=A0=A0=A0 // types of parameters<br>=A0=A0=A0 alias ParameterStora= geClassTuple!(func) storageTypes; // storage class of parameters<br> =A0=A0=A0 <br><br>=A0=A0=A0 static if (paramTypes.length !=3D args.length)<= br>=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0 pragma(msg, &quot;autoCall: Templat= e Error: Unmatched number of arguments for call to &quot; ~ __traits(identi= fier, func) ~ &quot;()&quot;);<br> =A0=A0=A0=A0=A0=A0=A0 static assert(0);<br>=A0=A0=A0 }<br>=A0=A0=A0 <br>=A0= =A0=A0 string declString, callString;<br>=A0=A0=A0 <br>=A0=A0=A0 // eg: cal= lString =3D &quot;funcName(&quot;<br>=A0=A0=A0 callString =3D __traits(iden= tifier, func) ~ &quot;(&quot;;<br>=A0=A0=A0 <br>=A0=A0=A0 foreach (int i, s= torageType; storageTypes)<br> =A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0 if (storageType =3D=3D STC.OUT || stor= ageType =3D=3D STC.REF)<br>=A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 // eg: declString ~=3D &quot;int newParamName; &quot;<br>=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 declString ~=3D paramTypes[i].stringof ~ &qu= ot; &quot; ~ args[i] ~ &quot;; &quot;;<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // = eg: callString =3D &quot;funcName(&quot; ~ &quot;newParamName,&quot;<br>=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 callString ~=3D &quot; &quot; ~ args[i] ~ &q= uot;,&quot;;<br>=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0=A0=A0=A0=A0 else<br>= =A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // Fetch the v= alue of args or args[i] and expand it to callString.<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // We cannot use the actual names of the = arguments since that would<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // not work= with literals (unless we added tests and more branching)<br>=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // need to use = static if here due to array bounds checking<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 static if (args.length =3D=3D 1)<br>=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0 // eg: callString =3D &quot;funcName(args, newParamName,&quot;<br>= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 callString ~=3D &#39;&quot;&#= 39; ~ to!string(args) ~ &#39;&quot;&#39; ~ &quot;,&quot;;<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 el= se<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0 // eg: callString =3D &quot;funcName(args[i], newParamName,= &quot;<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 callString ~=3D &#3= 9;&quot;&#39; ~ to!string(args[i]) ~ &#39;&quot;&#39; ~ &quot;,&quot;;<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0= }<br>=A0=A0=A0 <br>=A0=A0=A0 // remove the extra comma and close the call = string<br>=A0=A0=A0 callString =3D callString[0 .. $ - 1] ~ &quot;);&quot;;= <br><br>=A0=A0=A0 // return joined strings ready to be used with mixin()<br=

x, ref int z, string y)<br>{<br>=A0=A0=A0 writeln(x, y);<br>=A0=A0=A0 z = =3D 5;<br>}<br><br>unittest<br>{=A0=A0=A0 <br>=A0=A0=A0 immutable string va= r1 =3D &quot;foo&quot;;<br>=A0=A0=A0 immutable string var2 =3D &quot;bar&qu= ot;;<br> =A0=A0=A0 <br>=A0=A0=A0 mixin(autoCall!(testMe)(var1, &quot;newvar&quot;, v= ar2));<br>=A0=A0=A0 assert(newvar =3D=3D 5);<br>}<br><br>void main() { }<br=
<br>Fun stuff, right? :p<br><br><div class=3D"gmail_quote">On Sat, Aug 7, =

drej.mitrovich gmail.com">andrej.mitrovich gmail.com</a>&gt;</span> wrote:<= br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Cool stuff! I&#39= ;ll have a look later. Thanks.<div><div></div><div class=3D"h5"><br><br><di= v class=3D"gmail_quote"> On Sat, Aug 7, 2010 at 9:53 PM, Nick Sabalausky <span dir=3D"ltr">&lt;a a.a= &gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin: 0p= t 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1= ex;"> &quot;Andrej Mitrovic&quot; &lt;<a href=3D"mailto:andrej.mitrovich gmail.co= m" target=3D"_blank">andrej.mitrovich gmail.com</a>&gt; wrote in message<br=

<div>&gt; You know, I just had an idea. It would be really cool to have som= e sort of<br> &gt; generalized template function which can test other functions in variou= s<br> &gt; ways. You could pass strings as options, where you might choose the ty= pe<br> &gt; of<br> &gt; testing being done, such as verification tests or performance tests.. = Or<br> &gt; if<br> &gt; a function is designed to write a file on disk, maybe you&#39;d want t= o have<br> &gt; an<br> &gt; option for the maximum number of tests to run on that function (you do= n&#39;t<br> &gt; want to be left with 10000 files written in some temp directory<br> &gt; somewhere..), or a test to see if a function will fail with invalid in= put,<br> &gt; etc.<br> &gt;<br> &gt; Well it&#39;s just an idea, I better take a look at the existing test<= br> &gt; frameworks<br> &gt; and see how it&#39;s done there.<br> &gt;<br> <br> </div>Probably not quite as fancy as what you&#39;re talking about, but my = SemiTwist D<br> Tools library has a module that includes something that&#39;s similar to as= sert<br> but:<br> <br> - Allows to you verify that a particular statement throws a particular type= <br> of exception.<br> - Properly reports if an expression throws (and you didn&#39;t expect it to= ).<br> - Doesn&#39;t abort the program on failure.<br> - Reports &quot;expected&quot; expression and &quot;actual&quot; value.<br> <br> <a href=3D"http://www.dsource.org/projects/semitwist/browser/trunk/src/semi= twist/apps/tests/deferAssertTest/main.d" target=3D"_blank">http://www.dsour= ce.org/projects/semitwist/browser/trunk/src/semitwist/apps/tests/deferAsser= tTest/main.d</a><br> <a href=3D"http://www.dsource.org/projects/semitwist/browser/trunk/src/semi= twist/util/deferAssert.d" target=3D"_blank">http://www.dsource.org/projects= /semitwist/browser/trunk/src/semitwist/util/deferAssert.d</a><br> <br> It does need some clean-up and improvements, but it at least works.<br> (Although, trunk is currently in the process of switching from D1/Tango to<= br> D2/Phobos, so the latest revisions might be broken and it definitely doesn&= #39;t<br> take advantage of D2-specific features yet. But the version included with<b= r> Goldie 0.3 does work fine on D1/Tango though).<br> <br> <br> </blockquote></div><br> </div></div></blockquote></div><br> --e0cb4e8878274d9c9c048d416dbd--
Aug 07 2010
prev sibling parent "Nick Sabalausky" <a a.a> writes:
"Andrej Mitrovic" <andrej.mitrovich gmail.com> wrote in message 
news:i3f3a0$25fv$1 digitalmars.com...
 Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,

The key is that the argument to mixin() must be known at compile-time. When the compiler reaches a mixin(...), it looks at the argument to mixin and tries to evaluate it at compile-time, which may or may not be successful.
 because this will not compile:

 string input2 = "int y;";
 mixin(input2);

"string input2" is a run-time variable. When the compiler sees "mixin(input2)" it tries to evaluate the expression "input2" at compile-time. When it does that, it notices that input2 is mutable (ie, What if in between those two lines there was: "if(args.length > 3) input2 = someOtherString"?). Since the value can change at run-time, the compiler doesn't know at compile-time what the value will be. So it can't mix it in. True, in this particular case it could figure it out, but not in the general case, so it doesn't bother.
 But this will compile:

 immutable string input2 = "int y;";
 mixin(input2);

The compiler sees "mixin(input2)". It tries to evaluate the expression "input2" at compile-time. It sees that "input2" is immutable so it goes "Ah ha! That's *never* going to change, so I know exactly what the value is always going to be, so I can just plop it right in." Doing "enum input2" would also work for the same reason.
 And this too will compile:

 string returnString(string input)
 {
    return input;
 }

 mixin(returnString("int y;"));

The compiler reaches the mixin and tries to evaluate "returnString("int y;")" at compile-time. Thanks to CTFE, it's able to, so this works.
Aug 05 2010
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 05 Aug 2010 15:34:44 -0400, Tomek Sowiński <just ask.me> wrote:

 Andrej Mitrovic napisał:

 Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,
  because this will not compile:

 string input2 = "int y;";
 mixin(input2);

input2 is mutable, so theoretically there's no telling what value it holds.

Before you respond with more WTF, there is a subtlety here :) A string is aliased to immutable(char)[]. This means that the *data* pointed at is immutable but the *array* is mutable. You can reassign a string to point at some other immutable data, or change the length of the array. To illustrate this: string input2 = "int y;"; static this() { input2 = "int x;"; // perfectly legal } mixin(input2); // what does this do? Now, immutable string means: immutable(immutable(char)[]), which reduces to immutable(char[]), meaning both the data pointed at *and* the array are immutable (note the distinction of where the parentheses are). This can be used in CTFE and mixins because the compiler knows the value is completely defined at compile-time. strings (the kind where the array part is mutable) can be used as mixins as long as they are rvalues, such as the returns from functions which are CTFE capable. -Steve
Aug 05 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--001636418711d56d5f048d19577a
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Thanks, Steven!

You don't want to know what I'm up to :p.

I'm using some traits to find out what kind of parameters a function takes.
I'm also using a demangler from phobos to find out the name of the function
(But I can't find any straightforward way to get a name of a function
without getting back "1D_5std_5funcName" etc, so I just take a hardcoded
slice).

Anyway, I have a template function which takes as it's parameters a functio=
n
(aliased to func), and some arguments (right now just one in my hardcoded
code). It then creates a string which can be compiled via the mixin. It
constructs the string by checking the parameter types of func, and for any
ref or out parameter it detects it appends something like this to a string:
"long var1; long var2; long var3; ".

For any non-out/ref parameters it constructs another string which will call
that func (here is where I'm using the demangler to get the real name of th=
e
function). So the other string would eventually look like so:
"funcName(argN.., var1, var2, var3);

It then concatenates both strings and returns it, creating the whole
shebang:
"int var1; double var2; funcName(argN..., var1, var2, var3);"

This can then be used with mixin() at the calling site. I was inspiried to
try this out when bearophile wanted some new tuple syntax assignment in D.

So anyway, at the calling site I have this:

mixin(unpack!(getTimes)(r"C:\\cookies"));

getTimes() is a Phobos function with the signature:
getTimes(in char[] name, out d_time ftc, out d_time fta, out d_time ftm)

And now I automatically have var1, var2, and var3 at my disposal.

It was just an exercise for fun but it's cool that things like this are
possible in D. It would be nice if I could get the actual names of the
parameters the function takes + the clear name of the function itself, that
way I'd actually get back variables "ftc, fta, ftm" back).

Right now the code is a big mess but I could fix it up a lot and post it
here if anyone cares.

On Thu, Aug 5, 2010 at 9:48 PM, Steven Schveighoffer <schveiguy yahoo.com>w=
rote:

 On Thu, 05 Aug 2010 15:34:44 -0400, Tomek Sowi=C5=84ski <just ask.me> wro=

  Andrej Mitrovic napisa=C5=82:
  Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,
  because this will not compile:

 string input2 =3D "int y;";
 mixin(input2);

input2 is mutable, so theoretically there's no telling what value it holds.

Before you respond with more WTF, there is a subtlety here :) A string is aliased to immutable(char)[]. This means that the *data* pointed at is immutable but the *array* is mutable. You can reassign a string to point at some other immutable data, or change the length of the array. To illustrate this: string input2 =3D "int y;"; static this() { input2 =3D "int x;"; // perfectly legal } mixin(input2); // what does this do? Now, immutable string means: immutable(immutable(char)[]), which reduces to immutable(char[]), meaning both the data pointed at *and* the array are immutable (note the distinct=

 of where the parentheses are).  This can be used in CTFE and mixins becau=

 the compiler knows the value is completely defined at compile-time.

 strings (the kind where the array part is mutable) can be used as mixins =

 long as they are rvalues, such as the returns from functions which are CT=

 capable.

 -Steve

--001636418711d56d5f048d19577a Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Thanks, Steven!<br><br>You don&#39;t want to know what I&#39;m up to :p.<br=
<br>I&#39;m using some traits to find out what kind of parameters a functi=

f the function (But I can&#39;t find any straightforward way to get a name = of a function without getting back &quot;1D_5std_5funcName&quot; etc, so I = just take a hardcoded slice).<br> <br>Anyway, I have a template function which takes as it&#39;s parameters a= function (aliased to func), and some arguments (right now just one in my h= ardcoded code). It then creates a string which can be compiled via the mixi= n. It constructs the string by checking the parameter types of func, and fo= r any ref or out parameter it detects it appends something like this to a s= tring:<br> &quot;long var1; long var2; long var3; &quot;.<br><br>For any non-out/ref p= arameters it constructs another string which will call that func (here is w= here I&#39;m using the demangler to get the real name of the function). So = the other string would eventually look like so:<br> &quot;funcName(argN.., var1, var2, var3);<br><br>It then concatenates both = strings and returns it, creating the whole shebang:<br>&quot;int var1; doub= le var2; funcName(argN..., var1, var2, var3);&quot;<br><br>This can then be= used with mixin() at the calling site. I was inspiried to try this out whe= n bearophile wanted some new tuple syntax assignment in D.<br> <br>So anyway, at the calling site I have this:<br><br>mixin(unpack!(getTim= es)(r&quot;C:\\cookies&quot;));<br><br>getTimes() is a Phobos function with= the signature:<br>getTimes(in char[] name, out d_time ftc, out d_time fta,= out d_time ftm)<br> <br>And now I automatically have var1, var2, and var3 at my disposal. <br><= br>It was just an exercise for fun but it&#39;s cool that things like this = are possible in D. It would be nice if I could get the actual names of the = parameters the function takes + the clear name of the function itself, that= way I&#39;d actually get back variables &quot;ftc, fta, ftm&quot; back).<b= r> <br>Right now the code is a big mess but I could fix it up a lot and post i= t here if anyone cares.<br><br><div class=3D"gmail_quote">On Thu, Aug 5, 20= 10 at 9:48 PM, Steven Schveighoffer <span dir=3D"ltr">&lt;<a href=3D"mailto= :schveiguy yahoo.com">schveiguy yahoo.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class=3D"im"=
On Thu, 05 Aug 2010 15:34:44 -0400, Tomek Sowi=C5=84ski &lt;<a href=3D"mai=

<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Andrej Mitrovic napisa=C5=82:<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Hmm.. ok. Adding immutable helps. I&#39;m still a bit confused tho,<br> =C2=A0because this will not compile:<br> <br> string input2 =3D &quot;int y;&quot;;<br> mixin(input2);<br> </blockquote> <br> input2 is mutable, so theoretically there&#39;s no telling what value it ho= lds.<br> </blockquote> <br></div> Before you respond with more WTF, there is a subtlety here :)<br> <br> A string is aliased to immutable(char)[]. =C2=A0This means that the *data* = pointed at is immutable but the *array* is mutable. =C2=A0You can reassign = a string to point at some other immutable data, or change the length of the= array. =C2=A0To illustrate this:<br> <br> string input2 =3D &quot;int y;&quot;;<br> <br> static this()<br> {<br> =C2=A0 input2 =3D &quot;int x;&quot;; // perfectly legal<br> }<br> <br> mixin(input2); // what does this do?<br> <br> Now, immutable string means:<br> <br> immutable(immutable(char)[]), which reduces to immutable(char[]), meaning b= oth the data pointed at *and* the array are immutable (note the distinction= of where the parentheses are). =C2=A0This can be used in CTFE and mixins b= ecause the compiler knows the value is completely defined at compile-time.<= br> <br> strings (the kind where the array part is mutable) can be used as mixins as= long as they are rvalues, such as the returns from functions which are CTF= E capable.<br> <br> -Steve<br> </blockquote></div><br> --001636418711d56d5f048d19577a--
Aug 05 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5b741266065048d1a3ebe
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Aug 5, 2010 at 22:24, Andrej Mitrovic <andrej.mitrovich gmail.com>wrote:

 Thanks, Steven!

 You don't want to know what I'm up to :p.

Yes, yes, wo do!
 I'm using some traits to find out what kind of parameters a function takes.
 I'm also using a demangler from phobos to find out the name of the function
 (But I can't find any straightforward way to get a name of a function
 without getting back "1D_5std_5funcName" etc, so I just take a hardcoded
 slice).

You can get the name of an alias using __traits(identifier, aliasName), like this for example: template Name(alias a) { enum string Name = __traits(identifier, a); } int foo(int i) { return 0;} import std.stdio; void main() { writeln(Name!foo); // "foo", not "a". } As for demangling, how do you do to get mangled names in the first place?
 Anyway, I have a template function which takes as it's parameters a
 function (aliased to func), and some arguments (right now just one in my
 hardcoded code). It then creates a string which can be compiled via the
 mixin. It constructs the string by checking the parameter types of func, and
 for any ref or out parameter it detects it appends something like this to a
 string:
 "long var1; long var2; long var3; ".

How can you know if a parameter is ref or out? I remember seeing something about it in Phobos svn, are you using it?
 So anyway, at the calling site I have this:

 mixin(unpack!(getTimes)(r"C:\\cookies"));

 getTimes() is a Phobos function with the signature:
 getTimes(in char[] name, out d_time ftc, out d_time fta, out d_time ftm)

 And now I automatically have var1, var2, and var3 at my disposal.

That's fun :)
 It was just an exercise for fun but it's cool that things like this are
 possible in D. It would be nice if I could get the actual names of the
 parameters the function takes + the clear name of the function itself, that
 way I'd actually get back variables "ftc, fta, ftm" back)

I'm pretty sure I saw some code to do this. But maybe that was a D1 thing, using mangled names, too. in dsource/scrapple? Philippe --001636c5b741266065048d1a3ebe Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Thu, Aug 5, 2010 at 22:24, Andrej Mit= rovic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gmail.com">a= ndrej.mitrovich gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gma= il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef= t:1ex;"> Thanks, Steven!<br><br>You don&#39;t want to know what I&#39;m up to :p.<br=
</blockquote><div><br></div><div>Yes, yes, wo do!</div><div>=A0</div><bloc=

c solid;padding-left:1ex;"> <br>I&#39;m using some traits to find out what kind of parameters a functio= n takes. I&#39;m also using a demangler from phobos to find out the name of= the function (But I can&#39;t find any straightforward way to get a name o= f a function without getting back &quot;1D_5std_5funcName&quot; etc, so I j= ust take a hardcoded slice).<br> </blockquote><div><br></div><div>You can get the name of an alias using __t= raits(identifier, aliasName), like this for example:</div><div><br></div><d= iv>template Name(alias a)</div><div>{</div><div>=A0=A0enum string Name =3D = __traits(identifier, a);</div> <div>}</div><div><br></div><div>int foo(int i) { return 0;}</div><div><br><= /div><div>import std.stdio;</div><div>void main()</div><div>{</div><div>=A0= =A0writeln(Name!foo); // &quot;foo&quot;, not &quot;a&quot;.</div><div>}</d= iv> <div><br></div><div><br></div><div>As for demangling, how do you do to get = mangled names in the first place?</div><div>=A0</div><blockquote class=3D"g= mail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-l= eft:1ex;"> <br>Anyway, I have a template function which takes as it&#39;s parameters a= function (aliased to func), and some arguments (right now just one in my h= ardcoded code). It then creates a string which can be compiled via the mixi= n. It constructs the string by checking the parameter types of func, and fo= r any ref or out parameter it detects it appends something like this to a s= tring:<br> &quot;long var1; long var2; long var3; &quot;.<br></blockquote><div><br></d= iv><div>How can you know if a parameter is ref or out?</div><div>I remember= seeing something about it in Phobos svn, are you using it?</div><div> <br></div><div>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0= 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">So anyway, at the c= alling site I have this:<br><br>mixin(unpack!(getTimes)(r&quot;C:\\cookies&= quot;));<br> <br>getTimes() is a Phobos function with the signature:<br>getTimes(in char= [] name, out d_time ftc, out d_time fta, out d_time ftm)<br> <br>And now I automatically have var1, var2, and var3 at my disposal. <br><= /blockquote><div><br></div><div>That&#39;s fun :)</div><div><br></div><div>= =A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;borde= r-left:1px #ccc solid;padding-left:1ex;"> <br>It was just an exercise for fun but it&#39;s cool that things like this= are possible in D. It would be nice if I could get the actual names of the= parameters the function takes + the clear name of the function itself, tha= t way I&#39;d actually get back variables &quot;ftc, fta, ftm&quot; back)</= blockquote> <div><br></div><div>I&#39;m pretty sure I saw some code to do this. But may= be that was a D1 thing, using mangled names, too.</div><div>in dsource/scra= pple?</div><div><br></div><div>Philippe</div><div><br></div></div> --001636c5b741266065048d1a3ebe--
Aug 05 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--0016364c71adf865d4048d1a88dc
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Aug 5, 2010 at 11:29 PM, Philippe Sigaud
<philippe.sigaud gmail.com>wrote:

 On Thu, Aug 5, 2010 at 22:24, Andrej Mitrovic
<andrej.mitrovich gmail.com>wrote:

 Thanks, Steven!

 You don't want to know what I'm up to :p.

Yes, yes, wo do!
 I'm using some traits to find out what kind of parameters a function
 takes. I'm also using a demangler from phobos to find out the name of the
 function (But I can't find any straightforward way to get a name of a
 function without getting back "1D_5std_5funcName" etc, so I just take a
 hardcoded slice).

You can get the name of an alias using __traits(identifier, aliasName), like this for example: template Name(alias a) { enum string Name = __traits(identifier, a); } int foo(int i) { return 0;} import std.stdio; void main() { writeln(Name!foo); // "foo", not "a". } As for demangling, how do you do to get mangled names in the first place?

I was using mangledName!() from std.straits. __traits works prefectly, Thanks!
 Anyway, I have a template function which takes as it's parameters a
 function (aliased to func), and some arguments (right now just one in my
 hardcoded code). It then creates a string which can be compiled via the
 mixin. It constructs the string by checking the parameter types of func, and
 for any ref or out parameter it detects it appends something like this to a
 string:
 "long var1; long var2; long var3; ".

How can you know if a parameter is ref or out? I remember seeing something about it in Phobos svn, are you using it?

ParameterTypeTuple!(alias) - for the types ParameterStorageClassTuple!(alias) - for the storage class
 So anyway, at the calling site I have this:

 mixin(unpack!(getTimes)(r"C:\\cookies"));

 getTimes() is a Phobos function with the signature:
 getTimes(in char[] name, out d_time ftc, out d_time fta, out d_time ftm)

 And now I automatically have var1, var2, and var3 at my disposal.

That's fun :)
 It was just an exercise for fun but it's cool that things like this are
 possible in D. It would be nice if I could get the actual names of the
 parameters the function takes + the clear name of the function itself, that
 way I'd actually get back variables "ftc, fta, ftm" back)

I'm pretty sure I saw some code to do this. But maybe that was a D1 thing, using mangled names, too. in dsource/scrapple? Philippe

with Tango but it left me wanting more so I never stuck around much). But D2 is super-fun. --0016364c71adf865d4048d1a88dc Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Thu, Aug 5, 2010 at 11:29 PM, Philippe Sigaud= <span dir=3D"ltr">&lt;<a href=3D"mailto:philippe.sigaud gmail.com">philipp= e.sigaud gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quot= e" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204,= 204); padding-left: 1ex;"> <br><br><div class=3D"gmail_quote"><div class=3D"im">On Thu, Aug 5, 2010 at= 22:24, Andrej Mitrovic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitr= ovich gmail.com" target=3D"_blank">andrej.mitrovich gmail.com</a>&gt;</span=
 wrote:<br>

r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> Thanks, Steven!<br><br>You don&#39;t want to know what I&#39;m up to :p.<br=
</blockquote><div><br></div></div><div>Yes, yes, wo do!</div><div class=3D=

pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;= "> <br>I&#39;m using some traits to find out what kind of parameters a functio= n takes. I&#39;m also using a demangler from phobos to find out the name of= the function (But I can&#39;t find any straightforward way to get a name o= f a function without getting back &quot;1D_5std_5funcName&quot; etc, so I j= ust take a hardcoded slice).<br> </blockquote><div><br></div></div><div>You can get the name of an alias usi= ng __traits(identifier, aliasName), like this for example:</div><div><br></= div><div>template Name(alias a)</div><div>{</div><div>=A0=A0enum string Nam= e =3D __traits(identifier, a);</div> <div>}</div><div><br></div><div>int foo(int i) { return 0;}</div><div class= =3D"im"><div><br></div><div>import std.stdio;</div><div>void main()</div><d= iv>{</div></div><div>=A0=A0writeln(Name!foo); // &quot;foo&quot;, not &quot= ;a&quot;.</div> <div>}</div> <div><br></div><div><br></div><div>As for demangling, how do you do to get = mangled names in the first place?</div><div class=3D"im"><div>=A0</div></di= v></div></blockquote><div><br>I was using mangledName!() from std.straits. = __traits works prefectly, Thanks!<br> <br>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt= 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div= class=3D"gmail_quote"><div class=3D"im"><blockquote class=3D"gmail_quote" = style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 20= 4); padding-left: 1ex;"> <br>Anyway, I have a template function which takes as it&#39;s parameters a= function (aliased to func), and some arguments (right now just one in my h= ardcoded code). It then creates a string which can be compiled via the mixi= n. It constructs the string by checking the parameter types of func, and fo= r any ref or out parameter it detects it appends something like this to a s= tring:<br> &quot;long var1; long var2; long var3; &quot;.<br></blockquote><div><br></d= iv></div><div>How can you know if a parameter is ref or out?</div><div>I re= member seeing something about it in Phobos svn, are you using it?</div> <div class=3D"im"><div> <br></div></div></div></blockquote><div><br>Not svn, it&#39;s in 2.047 (may= be in earlier ones as well) in std.traits:<br><br>ParameterTypeTuple!(alias= ) - for the types<br>ParameterStorageClassTuple!(alias) - for the storage c= lass<br> <br>=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt= 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div= class=3D"gmail_quote"><div class=3D"im"><div></div><div>=A0</div><blockquo= te class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1= px solid rgb(204, 204, 204); padding-left: 1ex;"> So anyway, at the calling site I have this:<br><br>mixin(unpack!(getTimes)(= r&quot;C:\\cookies&quot;));<br> <br>getTimes() is a Phobos function with the signature:<br>getTimes(in char= [] name, out d_time ftc, out d_time fta, out d_time ftm)<br> <br>And now I automatically have var1, var2, and var3 at my disposal. <br><= /blockquote><div><br></div></div><div>That&#39;s fun :)</div><div class=3D"= im"><div><br></div><div>=A0</div><blockquote class=3D"gmail_quote" style=3D= "margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padd= ing-left: 1ex;"> <br>It was just an exercise for fun but it&#39;s cool that things like this= are possible in D. It would be nice if I could get the actual names of the= parameters the function takes + the clear name of the function itself, tha= t way I&#39;d actually get back variables &quot;ftc, fta, ftm&quot; back)</= blockquote> <div><br></div></div><div>I&#39;m pretty sure I saw some code to do this. B= ut maybe that was a D1 thing, using mangled names, too.</div><div>in dsourc= e/scrapple?</div><div><br></div><div>Philippe</div><div><br></div></div> </blockquote><div><br>Dunno, I haven&#39;t been using D1 really (actually I= tried it some years ago with Tango but it left me wanting more so I never = stuck around much). But D2 is super-fun.<br><br><br></div></div><br> --0016364c71adf865d4048d1a88dc--
Aug 05 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--001636c5b9b9b33ecb048d1ac0d9
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Aug 5, 2010 at 23:50, Andrej Mitrovic <andrej.mitrovich gmail.com>wrote:
 As for demangling, how do you do to get mangled names in the first place?

I was using mangledName!() from std.straits. __traits works prefectly, Thanks!

I didn't specifically look at this module for 2.047, my mistake. And, demange(mangledName!foo) gives back a qualified name, nice! I was looking as a way to get that, not two hours ago. *goes play with it* Ah, it doesn't work with templates names, though. Thanks Andrej! Philippe --001636c5b9b9b33ecb048d1ac0d9 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Thu, Aug 5, 2010 at 23:50, Andrej Mitrovic <s= pan dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gmail.com">andrej.mi= trovich gmail.com</a>&gt;</span> wrote:<blockquote class=3D"gmail_quote" st= yle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> <div class=3D"gmail_quote"><div class=3D"im"><blockquote class=3D"gmail_quo= te" style=3D"margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204, 204, 2= 04);padding-left:1ex"><div class=3D"gmail_quote"> <div><br></div><div><br></div><div>As for demangling, how do you do to get = mangled names in the first place?</div><div><div>=A0</div></div></div></blo= ckquote></div><div><br>I was using mangledName!() from std.straits. __trait= s works prefectly, Thanks!<br> <br></div></div></blockquote><div><br></div><div>Oh, oh, lots of shiny new = things in std.traits!=A0</div><div>I didn&#39;t specifically look at this m= odule for 2.047, my mistake.</div><div><br></div><div>And, demange(mangledN= ame!foo) gives back a qualified name, nice! I was looking as a way to get t= hat, not two hours ago.</div> <div><br></div><div>*goes play with it*</div><div><br></div><div>Ah, it doe= sn&#39;t work with templates names, though.</div><div><br></div><div>Thanks= Andrej!</div><div><br></div><div>Philippe</div><div><br></div></div> --001636c5b9b9b33ecb048d1ac0d9--
Aug 05 2010
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--001636e1ef9d944216048d1b1472
Content-Type: text/plain; charset=ISO-8859-1

Hope you have fun with it!

Anyway, here's a much cleaner edition of my magic little template function.
There's probably a host of errors and silly mistakes, but this is just a
proof of concept more than anything else, really:

import std.file;        // getTimes()
import std.conv;        // to!string()
import std.traits;      // Parameter types, storage, introspection

import std.stdio;

// Note:
// Errors are messy, since the function returns an arbitrary string ready to
be
// mixed in.
//
// If there are not enough arguments in args to make the call, autoCall
// will re-send the same argument value for each one missing in func's
signature.
// (this is by accident, not design)
//
// Tests could be added to make sure there are enough arguments in the
// args tuple however.
auto autoCall(alias func, T...)(T args)
{
    alias ParameterStorageClass STC;                // storage class enum
    alias ParameterTypeTuple!(func) types;          // types of parameters
    alias ParameterStorageClassTuple!(func) params; // storage class of
parameters

    string declString, callString;
    callString = __traits(identifier, func) ~ "(";  // funcName(

    foreach (int i, param; params)
    {
        if (param == STC.OUT || param == STC.REF)
        {
            // e.g. "int var1; "
            declString ~= types[i].stringof ~ " var" ~ i.stringof ~ "; ";

            // e.g. "funcName(" ~= " var1,"
            callString ~= " var" ~ i.stringof ~ ",";
        }
        else
        {
            // The non-out & non-ref parameter to func is already present in
args,
            // fetch it's value and append it to the call string.

            // need to use static if due to DMD complaints about array
bounds
            // in the else clause
            static if (args.length == 1)
            {
                // e.g. "funcName(" ~= "r'filename', "
                callString ~= '"' ~ to!string(args) ~ '"' ~ ",";
            }
            else
            {
                callString ~= '"' ~ to!string(args[i]) ~ '"' ~ ",";
            }
        }
    }

    // remove the remaining comma and close the call string
    callString = callString[0 .. $ - 1] ~ ");";

    // return joined strings ready to be mixin()'ed
    return declString ~= callString;
}

void testMe(string x, string y)
{
    writeln(x, y);
}

unittest
{
    mixin(autoCall!(getTimes)(r"C:\\cookies"));
    mixin(autoCall!(testMe)("test"));

    writefln("var1: %s, var2: %s, var3: %s", var1, var2, var3);
}

void main() { }



You'll have to replace the string in the first mixin ("cookies") to some
file on your drive. I'm not sure if the Linux version of getTimes works the
same way though.


On Fri, Aug 6, 2010 at 12:05 AM, Philippe Sigaud
<philippe.sigaud gmail.com>wrote:

 On Thu, Aug 5, 2010 at 23:50, Andrej Mitrovic
<andrej.mitrovich gmail.com>wrote:
 As for demangling, how do you do to get mangled names in the first place?

I was using mangledName!() from std.straits. __traits works prefectly, Thanks!

I didn't specifically look at this module for 2.047, my mistake. And, demange(mangledName!foo) gives back a qualified name, nice! I was looking as a way to get that, not two hours ago. *goes play with it* Ah, it doesn't work with templates names, though. Thanks Andrej! Philippe

--001636e1ef9d944216048d1b1472 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hope you have fun with it!<br><br>Anyway, here&#39;s a much cleaner edition= of my magic little template function. There&#39;s probably a host of error= s and silly mistakes, but this is just a proof of concept more than anythin= g else, really:<br> <br>import std.file;=A0=A0=A0=A0=A0=A0=A0 // getTimes()<br>import std.conv;= =A0=A0=A0=A0=A0=A0=A0 // to!string()<br>import std.traits;=A0=A0=A0=A0=A0 /= / Parameter types, storage, introspection<br><br>import std.stdio;<br><br>/= / Note:<br>// Errors are messy, since the function returns an arbitrary str= ing ready to be<br> // mixed in.<br>//<br>// If there are not enough arguments in args to make = the call, autoCall<br>// will re-send the same argument value for each one = missing in func&#39;s signature.<br>// (this is by accident, not design)<br=

e <br>// args tuple however.<br>auto autoCall(alias func, T...)(T args)<br>= {<br>=A0=A0=A0 alias ParameterStorageClass STC;=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 // storage class enum<br> =A0=A0=A0 alias ParameterTypeTuple!(func) types;=A0=A0=A0=A0=A0=A0=A0=A0=A0= // types of parameters<br>=A0=A0=A0 alias ParameterStorageClassTuple!(func= ) params; // storage class of parameters<br>=A0=A0=A0 <br>=A0=A0=A0 string = declString, callString;<br>=A0=A0=A0 callString =3D __traits(identifier, fu= nc) ~ &quot;(&quot;;=A0 // funcName( <br> =A0=A0=A0 <br>=A0=A0=A0 foreach (int i, param; params)<br>=A0=A0=A0 {<br>= =A0=A0=A0=A0=A0=A0=A0 if (param =3D=3D STC.OUT || param =3D=3D STC.REF)<br>= =A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // e.g. &quot;= int var1; &quot;<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 declString ~=3D types= [i].stringof ~ &quot; var&quot; ~ i.stringof ~ &quot;; &quot;;<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 <br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // = e.g. &quot;funcName(&quot; ~=3D &quot; var1,&quot;<br>=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 callString ~=3D &quot; var&quot; ~ i.stringof ~ &quot;,&quot;;= <br>=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0=A0=A0=A0=A0 else<br>=A0=A0=A0=A0= =A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // The non-out &amp; non-r= ef parameter to func is already present in args,<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // fetch it&#39;s value and append it to = the call string.<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 <br>=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0 // need to use static if due to DMD complaints about arr= ay bounds<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // in the else clause<br>=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 static if (args.length =3D=3D 1)<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 // e.g. &quot;funcName(&quot; ~=3D &quot;r&#39;filename&#39;, &qu= ot;<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 callString ~=3D &#39;&= quot;&#39; ~ to!string(args) ~ &#39;&quot;&#39; ~ &quot;,&quot;;<br>=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 }<br> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 else<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= {<br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 callString ~=3D &#39;&q= uot;&#39; ~ to!string(args[i]) ~ &#39;&quot;&#39; ~ &quot;,&quot;;<br>=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0=A0=A0=A0=A0 }<br>=A0=A0=A0 }<= br>=A0=A0=A0 <br>=A0=A0=A0 // remove the remaining comma and close the call= string<br> =A0=A0=A0 callString =3D callString[0 .. $ - 1] ~ &quot;);&quot;;<br><br>= =A0=A0=A0 // return joined strings ready to be mixin()&#39;ed<br>=A0=A0=A0 = return declString ~=3D callString;<br>}<br><br>void testMe(string x, string= y)<br>{<br>=A0=A0=A0 writeln(x, y);<br> }<br><br>unittest<br>{=A0=A0=A0 <br>=A0=A0=A0 mixin(autoCall!(getTimes)(r&q= uot;C:\\cookies&quot;));<br>=A0=A0=A0 mixin(autoCall!(testMe)(&quot;test&qu= ot;));<br>=A0=A0=A0 <br>=A0=A0=A0 writefln(&quot;var1: %s, var2: %s, var3: = %s&quot;, var1, var2, var3);<br> }<br><br>void main() { }<br><br><br><br>You&#39;ll have to replace the stri= ng in the first mixin (&quot;cookies&quot;) to some file on your drive. I&#= 39;m not sure if the Linux version of getTimes works the same way though.<b= r> <br><br><div class=3D"gmail_quote">On Fri, Aug 6, 2010 at 12:05 AM, Philipp= e Sigaud <span dir=3D"ltr">&lt;<a href=3D"mailto:philippe.sigaud gmail.com"=
philippe.sigaud gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gm=

04, 204, 204); padding-left: 1ex;"> <div class=3D"gmail_quote"><div class=3D"im">On Thu, Aug 5, 2010 at 23:50, = Andrej Mitrovic <span dir=3D"ltr">&lt;<a href=3D"mailto:andrej.mitrovich gm= ail.com" target=3D"_blank">andrej.mitrovich gmail.com</a>&gt;</span> wrote:= <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> <div class=3D"gmail_quote"><div><blockquote class=3D"gmail_quote" style=3D"= margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); paddi= ng-left: 1ex;"><div class=3D"gmail_quote"> <div><br></div><div><br></div><div>As for demangling, how do you do to get = mangled names in the first place?</div><div><div>=A0</div></div></div></blo= ckquote></div><div><br>I was using mangledName!() from std.straits. __trait= s works prefectly, Thanks!<br> <br></div></div></blockquote><div><br></div></div><div>Oh, oh, lots of shin= y new things in std.traits!=A0</div><div>I didn&#39;t specifically look at = this module for 2.047, my mistake.</div><div><br></div><div>And, demange(ma= ngledName!foo) gives back a qualified name, nice! I was looking as a way to= get that, not two hours ago.</div> <div><br></div><div>*goes play with it*</div><div><br></div><div>Ah, it doe= sn&#39;t work with templates names, though.</div><div><br></div><div>Thanks= Andrej!</div><div><br></div><div>Philippe</div><div><br></div></div> </blockquote></div><br> --001636e1ef9d944216048d1b1472--
Aug 05 2010
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
--00163646d2bc928159048d2908f7
Content-Type: text/plain; charset=ISO-8859-1

I've already tried that. But .mangleof on an aliased symbol just returns
"alias" as a string.


On Fri, Aug 6, 2010 at 10:44 AM, Jacob Carlborg <doob me.com> wrote:

 On 2010-08-05 23:50, Andrej Mitrovic wrote:

 On Thu, Aug 5, 2010 at 11:29 PM, Philippe Sigaud
 <philippe.sigaud gmail.com <mailto:philippe.sigaud gmail.com>> wrote:



    On Thu, Aug 5, 2010 at 22:24, Andrej Mitrovic
    <andrej.mitrovich gmail.com <mailto:andrej.mitrovich gmail.com>>
 wrote:

        Thanks, Steven!

        You don't want to know what I'm up to :p.


    Yes, yes, wo do!


        I'm using some traits to find out what kind of parameters a
        function takes. I'm also using a demangler from phobos to find
        out the name of the function (But I can't find any
        straightforward way to get a name of a function without getting
        back "1D_5std_5funcName" etc, so I just take a hardcoded slice).


    You can get the name of an alias using __traits(identifier,
    aliasName), like this for example:

    template Name(alias a)
    {
       enum string Name = __traits(identifier, a);
    }

    int foo(int i) { return 0;}

    import std.stdio;
    void main()
    {
       writeln(Name!foo); // "foo", not "a".
    }


    As for demangling, how do you do to get mangled names in the first
    place?


 I was using mangledName!() from std.straits. __traits works prefectly,
 Thanks!

You know there is a .mangleof property for all symbols. Anyway, I have a template function which takes as it's
        parameters a function (aliased to func), and some arguments
        (right now just one in my hardcoded code). It then creates a
        string which can be compiled via the mixin. It constructs the
        string by checking the parameter types of func, and for any ref
        or out parameter it detects it appends something like this to a
        string:
        "long var1; long var2; long var3; ".


    How can you know if a parameter is ref or out?
    I remember seeing something about it in Phobos svn, are you using it?


 Not svn, it's in 2.047 (maybe in earlier ones as well) in std.traits:

 ParameterTypeTuple!(alias) - for the types
 ParameterStorageClassTuple!(alias) - for the storage class

        So anyway, at the calling site I have this:

        mixin(unpack!(getTimes)(r"C:\\cookies"));

        getTimes() is a Phobos function with the signature:
        getTimes(in char[] name, out d_time ftc, out d_time fta, out
        d_time ftm)

        And now I automatically have var1, var2, and var3 at my disposal.


    That's fun :)


        It was just an exercise for fun but it's cool that things like
        this are possible in D. It would be nice if I could get the
        actual names of the parameters the function takes + the clear
        name of the function itself, that way I'd actually get back
        variables "ftc, fta, ftm" back)


    I'm pretty sure I saw some code to do this. But maybe that was a D1
    thing, using mangled names, too.
    in dsource/scrapple?

    Philippe


 Dunno, I haven't been using D1 really (actually I tried it some years
 ago with Tango but it left me wanting more so I never stuck around
 much). But D2 is super-fun.

-- /Jacob Carlborg

--00163646d2bc928159048d2908f7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I&#39;ve already tried that. But .mangleof on an aliased symbol just return= s &quot;alias&quot; as a string.<br><br><br><div class=3D"gmail_quote">On F= ri, Aug 6, 2010 at 10:44 AM, Jacob Carlborg <span dir=3D"ltr">&lt;<a href= =3D"mailto:doob me.com">doob me.com</a>&gt;</span> wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">On 2010-08-05 23:= 50, Andrej Mitrovic wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class=3D"im"=

&lt;<a href=3D"mailto:philippe.sigaud gmail.com" target=3D"_blank">philippe= .sigaud gmail.com</a> &lt;mailto:<a href=3D"mailto:philippe.sigaud gmail.co= m" target=3D"_blank">philippe.sigaud gmail.com</a>&gt;&gt; wrote:<br> <br> <br> <br> =A0 =A0On Thu, Aug 5, 2010 at 22:24, Andrej Mitrovic<br></div><div class= =3D"im"> =A0 =A0&lt;<a href=3D"mailto:andrej.mitrovich gmail.com" target=3D"_blank"=
andrej.mitrovich gmail.com</a> &lt;mailto:<a href=3D"mailto:andrej.mitrovi=

e:<br> <br> =A0 =A0 =A0 =A0Thanks, Steven!<br> <br> =A0 =A0 =A0 =A0You don&#39;t want to know what I&#39;m up to :p.<br> <br> <br> =A0 =A0Yes, yes, wo do!<br> <br> <br> =A0 =A0 =A0 =A0I&#39;m using some traits to find out what kind of paramete= rs a<br> =A0 =A0 =A0 =A0function takes. I&#39;m also using a demangler from phobos = to find<br> =A0 =A0 =A0 =A0out the name of the function (But I can&#39;t find any<br> =A0 =A0 =A0 =A0straightforward way to get a name of a function without get= ting<br> =A0 =A0 =A0 =A0back &quot;1D_5std_5funcName&quot; etc, so I just take a ha= rdcoded slice).<br> <br> <br> =A0 =A0You can get the name of an alias using __traits(identifier,<br> =A0 =A0aliasName), like this for example:<br> <br> =A0 =A0template Name(alias a)<br> =A0 =A0{<br> =A0 =A0 =A0 enum string Name =3D __traits(identifier, a);<br> =A0 =A0}<br> <br> =A0 =A0int foo(int i) { return 0;}<br> <br> =A0 =A0import std.stdio;<br> =A0 =A0void main()<br> =A0 =A0{<br> =A0 =A0 =A0 writeln(Name!foo); // &quot;foo&quot;, not &quot;a&quot;.<br> =A0 =A0}<br> <br> <br> =A0 =A0As for demangling, how do you do to get mangled names in the first<= br> =A0 =A0place?<br> <br> <br></div><div class=3D"im"> I was using mangledName!() from std.straits. __traits works prefectly,<br> Thanks!<br> </div></blockquote> <br> You know there is a .mangleof property for all symbols.<br> <br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class=3D"im"=

br> =A0 =A0 =A0 =A0parameters a function (aliased to func), and some arguments= <br> =A0 =A0 =A0 =A0(right now just one in my hardcoded code). It then creates = a<br> =A0 =A0 =A0 =A0string which can be compiled via the mixin. It constructs t= he<br> =A0 =A0 =A0 =A0string by checking the parameter types of func, and for any= ref<br> =A0 =A0 =A0 =A0or out parameter it detects it appends something like this = to a<br> =A0 =A0 =A0 =A0string:<br> =A0 =A0 =A0 =A0&quot;long var1; long var2; long var3; &quot;.<br> <br> <br> =A0 =A0How can you know if a parameter is ref or out?<br> =A0 =A0I remember seeing something about it in Phobos svn, are you using i= t?<br> <br> <br></div><div class=3D"im"> Not svn, it&#39;s in 2.047 (maybe in earlier ones as well) in std.traits:<b= r> <br> ParameterTypeTuple!(alias) - for the types<br> ParameterStorageClassTuple!(alias) - for the storage class<br> <br></div><div class=3D"im"> =A0 =A0 =A0 =A0So anyway, at the calling site I have this:<br> <br> =A0 =A0 =A0 =A0mixin(unpack!(getTimes)(r&quot;C:\\cookies&quot;));<br> <br> =A0 =A0 =A0 =A0getTimes() is a Phobos function with the signature:<br> =A0 =A0 =A0 =A0getTimes(in char[] name, out d_time ftc, out d_time fta, ou= t<br> =A0 =A0 =A0 =A0d_time ftm)<br> <br> =A0 =A0 =A0 =A0And now I automatically have var1, var2, and var3 at my dis= posal.<br> <br> <br> =A0 =A0That&#39;s fun :)<br> <br> <br> =A0 =A0 =A0 =A0It was just an exercise for fun but it&#39;s cool that thin= gs like<br> =A0 =A0 =A0 =A0this are possible in D. It would be nice if I could get the= <br> =A0 =A0 =A0 =A0actual names of the parameters the function takes + the cle= ar<br> =A0 =A0 =A0 =A0name of the function itself, that way I&#39;d actually get = back<br> =A0 =A0 =A0 =A0variables &quot;ftc, fta, ftm&quot; back)<br> <br> <br> =A0 =A0I&#39;m pretty sure I saw some code to do this. But maybe that was = a D1<br> =A0 =A0thing, using mangled names, too.<br> =A0 =A0in dsource/scrapple?<br> <br> =A0 =A0Philippe<br> <br> <br></div><div class=3D"im"> Dunno, I haven&#39;t been using D1 really (actually I tried it some years<b= r> ago with Tango but it left me wanting more so I never stuck around<br> much). But D2 is super-fun.<br> <br> <br> <br> </div></blockquote> <br> <br> -- <br><font color=3D"#888888"> /Jacob Carlborg<br> </font></blockquote></div><br> --00163646d2bc928159048d2908f7--
Aug 06 2010