alias this constructor
Adam D. Ruppe
destructionator at gmail.com
Sun Mar 25 03:06:11 UTC 2018
On Sunday, 25 March 2018 at 00:47:20 UTC, Dave Jones wrote:
> struct Foo
> {
> int x;
> alias x this;
> }
>
> Foo f = 12; // error
You just need to define a constructor that takes an int. alias
this is all about taking an existing object and substituting a
member for it - emphasis on *existing* object, so it needs to
already be constructed.
But you can construct from other types
struct Foo {
int x;
this(int x) { this.x = x; }
}
Foo f = 12; // allowed, calls that constructor
> Foo foo;
> f = 12; // this is OK
Important to note that foo already exists here and is NOT
reconstructed - the compiler just rewrites that into `f.x = 12;`,
doing a member assignment.
More information about the Digitalmars-d
mailing list