Mixin Expressions, can't evalutate string variable

Andrej Mitrovic andrej.mitrovich at gmail.com
Thu Aug 5 15:29:15 PDT 2010


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

> On Thu, Aug 5, 2010 at 23:50, Andrej Mitrovic <andrej.mitrovich at 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!
>>
>>
> Oh, oh, lots of shiny new things in std.traits!
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100806/192fa12f/attachment-0001.html>


More information about the Digitalmars-d mailing list