On Tuesday, 18 March 2014 at 23:10:05 UTC, Chris Williams wrote:
Annoying that formatting the string is required. I think you're right that it's ignoring my alias parameter because I'm not using it. Formatting doesn't accomplish anything except to use the parameter. But in exchange, you have to relate your format arguments to the %s entries, instead of the q{} contents just being code.


import fs = std.file;
import std.stdio;
import std.string: format;

template gotoPath(alias path) {
    enum gotoPath = format(q{
        string currPath = fs.getcwd();
        scope (exit) fs.chdir(currPath);
        fs.chdir(%s);
    }, __traits(identifier, path));
}

void doStuff(string targetDirectory) {
    mixin(gotoPath!(targetDirectory));
    writefln("b %s", fs.getcwd());
}

void main() {
    writefln("a %s", fs.getcwd());
    doStuff("/etc");
    writefln("c %s", fs.getcwd());
}


I feel like there might be a misunderstanding. It's not that the
parameter was ignored because it wasn't used. You're not giving
the compiler a little extra help.

You're building a string from the parameter. __traits(identifier,
path) isn't "path", it's "targetDirectory" (in this specific
instantiation).

Reply via email to