String mixin in templates

Andre andre at s-e-a-p.de
Tue Sep 18 09:28:23 PDT 2012


On Tuesday, 18 September 2012 at 16:13:49 UTC, Andrej Mitrovic
wrote:
> On 9/18/12, Andre <andre at s-e-a-p.de> wrote:
>> snip
>
> Templates introduce a new scope and in that scope 'var' doesn't 
> exist,
> what you want are template mixins (note: mixin expressions and
> template mixins are different things):
>
> mixin template test2(string str){
>         void test2(){
>                 mixin("writeln(" ~ str ~ ");");
>         }
> }
>
> void main() {
> mixin test2!"var";
> }
>
> Note that you don't have to pass the variable by a string, you 
> can use
> an 'alias' and get rid of that mixin altogether:
>
> mixin template test2(alias symb)
> {
>     void test2()
>     {
>         writeln(symb);
>     }
> }
>
> void main()
> {
>     string var = "Hello World!";
>     mixin test2!var;
> }

What I want to do is to have my own sub language entered as
string. The string contains a command, expressions and also
variables of the actual scope, which will be read or written to.
Therefore I need string mixins. I just wonder wheter there is a
possibility not to write at every place
"mixin(interpret("..."));" but a nice statement like
interpret!("...");

Kind regards
Andre


More information about the Digitalmars-d-learn mailing list