Request: Auto string mixin templates
JS
js.mdnq at gmail.com
Mon Jul 29 03:54:36 PDT 2013
I have a template t that returns a string that is always to be
used as a mixin.
It is quite annoying to have to use mixin() on the template.
e.g.,
mixin(t!());
It would be nice to be able to specify that the template is mixed
in at the call site.
e.g.,
string mixin template t() { ... }
then
t!(); // is equivalent to mixin(t!()); and no scope issues
The post at
http://forum.dlang.org/thread/wqmngajgejxreyzftrdw@forum.dlang.org#post-wqmngajgejxreyzftrdw:40forum.dlang.org
Has an example case where I have to call mixin(tJoin!(...)) to
generate the code to join the strings. This statement can't be
wrapped in way to simplify it without defeating the purpose of
the optimization it gives.
The problem is that when you use variables in a string mixin that
are outside the template definition and try to string mixin(to
hide the explicit mixin call), then those variables are outside
the scope making the call fail.
In the code above, tJoin takes a variadic set of arguments and
forms a string that concatenates all of them(just as if you typed
it by hand) but also deals with string arrays by inserting a call
to a RT function that joins the array.
so the output of tJoin is a code string that can be used to
concatenate all the arguments passed to it... e.g.,
tJoin!(":", a, b, "c") ==> `a~b~"c"`; if a and b are strings.
mixing in that string, e.g., mixin(tJoin!(":", a, b, "c")), will
concatenate a with b with c... but only if in the scope of a and
b. Hence, trying to wrap the mixin statement will change it's
scope and a and b will either be unknown or wrong.
Having someway to specify that the template should automatically
be mixed in would help solve these sorts of problems(I've ran
across it before when trying to generate code using a template
and avoid the mixin statement).
A few possible options are:
string mixin template t() { ... }
string template t() { ... }
auto mixin template t() { ... }
template t() { ... mixin enum t = ...; }
template t() { ... mixin t = ...; }
etc...
More information about the Digitalmars-d
mailing list