digitalmars.D.learn - string mixin and alias
- Andre Pany (15/15) Jul 28 2016 Hi,
- rikki cattermole (3/18) Jul 28 2016 Well those string mixins are not evaluating out to a symbol. So that
- pineapple (3/18) Jul 29 2016 It's not clear what you're trying to accomplish. What would you
- Andre Pany (7/30) Jul 29 2016 It is more or less syntax sugar. In the main function instead
- pineapple (11/17) Jul 29 2016 As far as I know, there's no way to hide away the `mixin`
- Steven Schveighoffer (6/16) Jul 29 2016 s is a runtime parameter. You can't mixin stuff that is only available
- Jesse Phillips (19/19) Jul 29 2016 D won't let you hide the mixin, the keyword is there specifically
- Jesse Phillips (16/20) Jul 29 2016 Here is a fully functioning example:
- Andre (4/24) Jul 29 2016 Thanks for all the answers.
Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards André
Jul 28 2016
On 29/07/2016 6:38 PM, Andre Pany wrote:Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards AndréWell those string mixins are not evaluating out to a symbol. So that could be a rather big problem for the Alias template.
Jul 28 2016
On Friday, 29 July 2016 at 06:38:17 UTC, Andre Pany wrote:Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards AndréIt's not clear what you're trying to accomplish. What would you expect this code to do?
Jul 29 2016
On Friday, 29 July 2016 at 12:11:44 UTC, pineapple wrote:On Friday, 29 July 2016 at 06:38:17 UTC, Andre Pany wrote:It is more or less syntax sugar. In the main function instead of writing "mixin(generateCode(s));" I want to write "foo(s);". So, the mixin statement is hidden while the functionality of mixin stays. Kind regards AndréHi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s))); alias foo2(string s) = Alias!(mixin(generateCode(s))); string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo(s); foo2(s); } Kind regards AndréIt's not clear what you're trying to accomplish. What would you expect this code to do?
Jul 29 2016
On Friday, 29 July 2016 at 12:22:54 UTC, Andre Pany wrote:It is more or less syntax sugar. In the main function instead of writing "mixin(generateCode(s));" I want to write "foo(s);". So, the mixin statement is hidden while the functionality of mixin stays. Kind regards AndréAs far as I know, there's no way to hide away the `mixin` statement in a template or alias. You could do something like this, if you really wanted to, but really the only difference is that it uses fewer parentheses. string generateCode(string s){return "";} enum foo(string s) = generateCode(s); void main(){ enum s = "int a = 2 + 3; int b = 4 + a;"; mixin foo!s; }
Jul 29 2016
On 7/29/16 2:38 AM, Andre Pany wrote:Hi, is there a way to alias a string mixin? Neither foo nor foo2 compiles. import std.meta : Alias; alias foo = (s) => Alias!(mixin(generateCode(s)));s is a runtime parameter. You can't mixin stuff that is only available at runtime.alias foo2(string s) = Alias!(mixin(generateCode(s)));This would work if the string is "main", let's say.string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;";This does not result in a symbol, an alias needs a symbol. -Steve
Jul 29 2016
D won't let you hide the mixin, the keyword is there specifically to indicate code is being injected in that location. This isn't what you are looking for, but you can do something like this: ------------- string generateCode(string s){return "";} void main() { enum s = "a = 2 + 3; b = 4 + a;"; foo!(s); } void foo(alias s)() { mixin(generateCode(s)); } ------------- Here the generateCode() is mixed in to the context of foo(), which is fine if your code is performing actions but is no good if you're creating declarations that you want to use in the context of main().
Jul 29 2016
On Friday, 29 July 2016 at 18:34:56 UTC, Jesse Phillips wrote:Here the generateCode() is mixed in to the context of foo(), which is fine if your code is performing actions but is no good if you're creating declarations that you want to use in the context of main().Here is a fully functioning example: ----------- string generateCode(string s){return s;} void main() { int a, b; enum s = "a = 2 + 3; b = 4 + a;"; foo!(s)(a, b); assert(a == 5); assert(b == 9); } void foo(alias s)(ref int a, ref int b) { mixin(generateCode(s)); } -----------
Jul 29 2016
On Friday, 29 July 2016 at 18:39:23 UTC, Jesse Phillips wrote:On Friday, 29 July 2016 at 18:34:56 UTC, Jesse Phillips wrote:Thanks for all the answers. Kind regards AndreHere the generateCode() is mixed in to the context of foo(), which is fine if your code is performing actions but is no good if you're creating declarations that you want to use in the context of main().Here is a fully functioning example: ----------- string generateCode(string s){return s;} void main() { int a, b; enum s = "a = 2 + 3; b = 4 + a;"; foo!(s)(a, b); assert(a == 5); assert(b == 9); } void foo(alias s)(ref int a, ref int b) { mixin(generateCode(s)); } -----------
Jul 29 2016