Emulation macros and pattern matching on D

Dennis Ritchie via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jun 5 07:15:08 PDT 2015


On Friday, 5 June 2015 at 13:13:15 UTC, Steven Schveighoffer 
wrote:
> string foo(string mode, string value)
> {
>    return `writefln("mode ` ~ mode ~ `: %s", ` ~ value ~ `);`;
> }
>
> void main()
> {
>    mixin(foo("Y", "3"));
>    mixin(foo("X", "2"));
> }

Thanks. It looks really simple, but I still do not understand the 
concept of using mixins in full.

I do not understand why in this line:
return `writefln("mode ` ~ mode ~ `: %s", ` ~ value ~ `);`;

use the following syntax:
~ mode ~ , ~ value ~

For example, why here I can simply write:

void main() {
     int b = 5;
     mixin(`int a = b;`);
     assert(a == 5);
}

Why should not I write like this:

void main() {
     int b = 5;
     mixin(`"int a = " ` ~ b ~ ` ";"`);
     assert(a == 5);
}


More information about the Digitalmars-d-learn mailing list