Mixin Expressions, can't evalutate string variable

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Aug 5 13:24:54 PDT 2010


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

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:
"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 at yahoo.com>wrote:

> On Thu, 05 Aug 2010 15:34:44 -0400, Tomek Sowiński <just at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100805/4b50c96d/attachment-0001.html>


More information about the Digitalmars-d mailing list