On 2013-08-19 00:31, JS wrote:
module main;
import std.stdio, std.conv;
template foo(alias T)
{
string foo(string s)
{
string x = to!string(T) ~ s ~ "yyyy";
//pragma(msg, x); // pragma see's x as a run time variable(or rather pragma is
executed before x is truly defined)
return x;
}
well, i think i understand what is going on in your example. here x is a
runtime value because 's' is passed as a function argument (always runtime
within this very function). template instantiation (this is when pragmas(msg)
is already called) comes first and at this time 's' is unknown, as the
instantiated function could be later called with whatever 's' - the compiler
cannot know what 's' at this point.
tricks i found around this:
a) if current structure of example program is required (i.s. definition of
foo), add a wrapper template for compile time printing:
template _dbg_foo(alias T, string s){
enum x = foo!(T)(s);
pragma(msg, x);
enum _dbg_foo = x;
}
b) if foo does not need to have 's' as an argument, make it a template
parameter, too. you also have to use an enum instead of a string for temporary
storage:
string foo2(alias T, string s)(){
enum x = to!string(T) ~ s ~ "yyyy";
pragma(msg, x);
return x;
}
the problems i currently face during CTFE are more like:
a) in some cases manifest enums are broken in that they don't contain working
compile time copy&paste values. this is currently the case for TypeTuples. i
think this will be fixed in the next DMD release.
b) pragma(msg) has not the same behavior as write() when it comes to formating
the output of non-string values, it sometimes needs help, e.g. with a
to!string() call.