www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string mixin and alias

reply Andre Pany <andre s-e-a-p.de> writes:
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
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
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
prev sibling next sibling parent reply pineapple <meapineapple gmail.com> writes:
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
parent reply Andre Pany <andre s-e-a-p.de> writes:
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:
 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?
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é
Jul 29 2016
parent pineapple <meapineapple gmail.com> writes:
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
prev sibling next sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
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
prev sibling parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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
parent reply Jesse Phillips <Jesse.K.Phillips+D gmail.com> writes:
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
parent Andre <andre s-e-a-p.de> writes:
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:
 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)); } -----------
Thanks for all the answers. Kind regards Andre
Jul 29 2016