`Alias this` to a mixed in property
Bastiaan Veelo
Bastiaan at Veelo.net
Wed Jan 24 14:35:08 UTC 2018
On Wednesday, 24 January 2018 at 14:21:42 UTC, ag0aep6g wrote:
> The spec says that you cannot make an overload set just by
> mixing in multiple functions/methods with the same name.
> Instead, you have to do it like this:
>
> ----
> mixin getter g;
> mixin setter!int s;
>
> alias p = g.p;
> alias p = s.p;
> ----
>
> https://dlang.org/spec/template-mixin.html#mixin_scope
Thanks a lot! I didn't know you could do overloads by way of
multiple aliases with the same name.
I meant to use this for mixing in multiple instantiations
generated from a static foreach over an AliasSeq of types, but
generating unique identifiers poses an extra challenge.
I may go for string mixin's instead, which I just discovered do
work:
```
import std.stdio;
enum getter = `
@property int p()
{
writeln(__LINE__, " mixin getter");
return 3;
}
`;
string setter(string T) pure
{ return `
@property int p(` ~ T ~ ` arg)
{
writeln(__LINE__, " mixin setter ` ~ T ~ `" , arg);
return 4;
}
`;
}
struct S
{
mixin(getter);
mixin(setter("int"));
alias p this;
}
void main(string[] args)
{
S s;
s = 7;
int i = s;
}
```
More information about the Digitalmars-d-learn
mailing list