digitalmars.D.learn - Convert mixin to function call
- IntegratedDimensions (48/48) May 20 2018 I have a string mixin that returns a value or function that uses
- IntegratedDimensions (3/3) May 20 2018 Also, one thing that would help would be able to create
- IntegratedDimensions (32/32) May 20 2018 https://dpaste.dzfl.pl/fb49bf834cff
- IntegratedDimensions (3/3) May 20 2018 It should be obvious that these are simplifications. I can't pass
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.
May 20 2018
Also, one thing that would help would be able to create identifier names that are unique to avoid collisions. Does D have any ability to do such a thing?
May 20 2018
https://dpaste.dzfl.pl/fb49bf834cff import std.stdio; auto Q(string A)() { auto foo() { auto d = mixin(Z!(A)()); return d; } return foo()(); } auto X(string A, string N)() { return N~" = (() { int y = 4; return "~A~" + y + 5; })();"; } auto Z(string A)() { return "() { int y = 4; return "~A~" + y + 5; }"; } void main() { int x = 3; double y = 0; pragma(msg, (X!("x", "y")())); mixin(X!("x", "y")()); y = Q!("x")(); writeln(y); } using Q is more ideomatic but foo does not have access to x which is required(since Q should behave like a string mixin as far as scope is concerned).
May 20 2018
It should be obvious that these are simplifications. I can't pass the variables directly as parameters as in the real case the names may only be partially specified.
May 20 2018