Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
July 29, 2016 string mixin and alias | ||||
---|---|---|---|---|
| ||||
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é |
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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.
|
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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?
|
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to pineapple | 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é
|
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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 |
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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;
}
|
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andre Pany | 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(). |
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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));
}
-----------
|
July 29, 2016 Re: string mixin and alias | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | 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
|
Copyright © 1999-2021 by the D Language Foundation