I have a string mixin that returns a value or function that uses
the mixed in scope.
Currently I have to wrap the mixin in a delegate or local
function as to be able to get the value:
int x = 3;
int y = 1;
auto foo() { mixin(X!("x")); }
This allows the the mixin to see the scope but keep the mixin
variables local so they do not shadow anything unintentionally.
auto X(string A)()
{
return "int y = 4; return "~A~" + y + 5;";
}
foo will just return 8 but I cannot simply mixin within the main
scope or the scope will return unintentionally.
int x = 3;
mixin(X!("x")) // <- hidden return
Using foo works fine but adds a level of indirection I feel is
unnecessary. The only thing I feel can work is to rewrite X so
that it uses variables with random names to reduce shadowing
issues and to allow X to take a variable name to put the result
in:
auto X(string A, string N)()
{
return "{ int _342sdfs = 4; "~N~" = "~A~" + _342sdfs + 5;}";
}
which complicates things significantly. My hope is that D would
inline the function call.
Ideally I'd like to be able to sort of use the mixin as a
function and simply call it:
val = mixin(X!("x"))();
which I could do by internally using a delegate inside the mixin
auto X(string A)()
{
return "(() { int y = 4; return "~A~" + y + 5; })";
}
except that the mixin syntax does not allow one to use it in an
expression.
Remember that the mixin must be able to see the scope it is mixed
in at so one can't wrap this in a template use it as far as I
know?
Although, one can do
mixin(X!("val", "x"));
which does the ugly work.
It would be nice to be able to get this process to look as much
like normal D code as possible.
the idea is to try to write "local" string mixins that are messy
and not "C'ish" looking in to "C'ish" looking code.