Thanks, Steven!<br><br>You don't want to know what I'm up to :p.<br><br>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).<br>
<br>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:<br>
"long var1; long var2; long var3; ".<br><br>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 the function). So the other string would eventually look like so:<br>
"funcName(argN.., var1, var2, var3);<br><br>It then concatenates both strings and returns it, creating the whole shebang:<br>"int var1; double var2; funcName(argN..., var1, var2, var3);"<br><br>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.<br>
<br>So anyway, at the calling site I have this:<br><br>mixin(unpack!(getTimes)(r"C:\\cookies"));<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'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).<br>
<br>Right now the code is a big mess but I could fix it up a lot and post it here if anyone cares.<br><br><div class="gmail_quote">On Thu, Aug 5, 2010 at 9:48 PM, Steven Schveighoffer <span dir="ltr"><<a href="mailto:schveiguy@yahoo.com">schveiguy@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div class="im">On Thu, 05 Aug 2010 15:34:44 -0400, Tomek Sowiński <<a href="mailto:just@ask.me" target="_blank">just@ask.me</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Andrej Mitrovic napisał:<br>
<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hmm.. ok. Adding immutable helps. I'm still a bit confused tho,<br>
because this will not compile:<br>
<br>
string input2 = "int y;";<br>
mixin(input2);<br>
</blockquote>
<br>
input2 is mutable, so theoretically there's no telling what value it holds.<br>
</blockquote>
<br></div>
Before you respond with more WTF, there is a subtlety here :)<br>
<br>
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:<br>
<br>
string input2 = "int y;";<br>
<br>
static this()<br>
{<br>
input2 = "int x;"; // 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 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.<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 CTFE capable.<br>
<br>
-Steve<br>
</blockquote></div><br>