Implicit conversion in constructor

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 20:06:22 PDT 2015


On Saturday, 18 July 2015 at 02:28:09 UTC, tcak wrote:
> I even am not sure how in the world it allows implicit 
> conversion from class to struct in "fine" at all.

Given:

struct Foo {
    T x;
    alias x this;
}


Any time you do

Foo foo;

foo.something = whatever;
// or
foo = whatever;
// or
whatever = foo;


It first tries that code directly. If that doesn't work, it then 
tries rewriting `foo` into `foo.x`:

foo.x.something = whatever;
foo.x = whatever;
whatever = foo.x;


And if that compiles, you're all set, that code gets generated.


The OP's situation is just case #2 here. The compiler is taking 
the illegal `thing = new Foo;` and rewriting it into the legal 
`thing.foo = new Foo;` automatically via alias this.


More information about the Digitalmars-d-learn mailing list