On Sunday, 26 October 2014 at 10:48:46 UTC, ketmar via Digitalmars-d-learn wrote:
Hello.

the following code is not working:

  template prstr(string s) {
    enum prstr = "write("~s.stringof~");\n";
  }

  string buildWriter() (string fmt) {
    return prstr!(fmt[0..$-1]);
  }

Your passing the runtime parameter `fmt` as template argument. Try this:

    string buildWriter(string fmt)() {
      return prstr!(fmt[0..$-1]);
    }


  string writer(string fmt) () {
    enum s = buildWriter(fmt);

and this:
      enum s = buildWriter!(fmt);

    return s;
  }

  void main () {
    import std.stdio;
    writeln(writer!"str"());
  }

  z40.d(6): Error: variable fmt cannot be read at compile time
z40.d(10): Error: template instance z40.buildWriter!() error instantiating
  z40.d(16):        instantiated from here: writer!"str"

but why? fmt is known in CTFE and compiler can use it as string literal
for instantiating `prstr`.

please, don't mind the idiocity of the code: this is just a sample to show what confuses me. i know about possibility of moving `fmt` to
template argument, but i need it as function argument, 'cause
`buildWriter()` actually does string processing and i want to
instantiate `prstr` with the part of the string. and this processing
cannot be done in 'foreach'.

and writing everything in functional style sux: it leads to explosive
growing of the number of arguments passed to templates.

Reply via email to