how to achieve C's Token Pasting (##) Operator to generate variable name in D?

Sebastiaan Koppe mail at skoppe.eu
Sun May 31 09:28:07 UTC 2020


On Saturday, 30 May 2020 at 23:39:31 UTC, mw wrote:
> Am I doing the right thing in D? any improvement you'd suggest?
>
> e.g. I don't quite like have to put the type and var name in 
> the quotes as string:
>
>   mixin(RW!("int",     "x"));
>
> Is there a better way to achieve this? esp. for the type `int`, 
> is there any way I don't have to quote it as string?
>
> Thanks.

This would also be an option.

```
import std;

class Point {
     struct Inner {
     	int x;
         double y;
     }
     private Inner inner;
     Point opDispatch(string name, T)(T value) {
         mixin("inner."~name~" = value;");
         return this;
     }
     auto opDispatch(string name)() {
         mixin("return inner."~name~";");
     }
}
void main()
{
     Point b = new Point();
     b.x(4).y(5.0);
     writeln(b.x);
}
```


More information about the Digitalmars-d-learn mailing list