I'm compiling the following application (2 d modules)

// file c/tool.d
module c.tool;

struct Tool
{
  string name;

  auto generate()
  {
    version (DOES_NOT_WORK)
    {
      import std.traits : hasMember;
      if (hasMember!(Tool, name))
return `writeln("this is a ` ~ mixin ("this." ~ name ~ "()") ~ `.");`;
    }
    else
    {
      if (name == "hammer")
        return `writeln("this is a ` ~ this.hammer() ~ `.");`;
    }
    return `writeln("unknown tool ` ~ name ~ `.");`;
  }

  auto hammer()
  {
    return "screwdriver";
  }
}

string code(string input)
{
  auto tool = Tool(input);
  return tool.generate();
}
-----------------
// file x/app.d
class Application
{
  void run()
  {
    import c.tool;
    import std.stdio;
    mixin (code("hammer"));
    mixin (code("transmogrifier"));
  }
}

int main(string[] args)
{
  auto app = new Application();
  app.run();
  return 0;
}

When I compile version DOES_NOT_WORK, I get the following error:
c/tool.d(13): Error: variable name cannot be read at compile time
c/tool.d(13): while looking for match for hasMember!(Tool, name)

However, the other version ('else' case) compiles, runs, and outputs (as expected):
this is a screwdriver.
unknown tool transmogrifier.

What's the fundamental difference that makes the variable 'name' readable in one version and unreadable in another? Should the version DOES_NOT_WORK not be compilable in principle or is it only a limitation of the current CTFE implementation in the front-end?

Thanks.

Reply via email to