Alias template param - Undefined Identifier

Chris Williams yoreanon-chrisw at yahoo.co.jp
Tue Mar 18 14:56:31 PDT 2014


I have a series of functions that each needs to change directory, 
perform an action, and then revert to the original working 
directory.

I could put a wrapper around the calls to these functions that 
performs this action, but I figured a little macro would be 
sufficient for my needs (it's just a little script.)


import fs = std.file;
import std.stdio;

template gotoPath(alias path) {
     enum gotoPath = q{
         string currPath = fs.getcwd();
         scope (exit) fs.chdir(currPath);
         fs.chdir(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());
}


When I try to compile (DMD 2.064), I get the following error:

test.d(16): Error: undefined identifier path

I can fix the issue by removing the alias parameter and 
hardcoding the name of the variable in the template:


import fs = std.file;
import std.stdio;

template gotoPath() {
     enum gotoPath = q{
         string currPath = fs.getcwd();
         scope (exit) fs.chdir(currPath);
         fs.chdir(targetDirectory);
     };
}

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

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


However, it seems like that shouldn't be necessary. The alias 
parameter should create a local alias for the external variable 
which can be utilized by the generated code. Is there something 
wrong with my syntax?


More information about the Digitalmars-d-learn mailing list