alias this and struct initializer
Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu May 18 05:56:09 PDT 2017
On Thursday, 18 May 2017 at 08:40:39 UTC, Andre Pany wrote:
> I think as case 2 is working case 3 should work also.
Nope, case 2 is assigning to an already constructed object and
case 3 is constructing a new one.
alias this is NEVER used in construction. It can only apply after
the object already exists, just like subclasses vs interfaces.
Once the object exists, you can assign a subclass to an
interface, but you can't do
SubClass obj = new Interface();
in theory, the compiler could see the left hand side and know it
is supposed to be SubClass, but it doesn't - you need to
construct the class explicitly.
Same with alias this, it allows implicit conversion TO the type
and assignment of the member through the existing variable (the
existing variable must already be valid, it is already
constructed, so it is no different than assigning any other
public member), but not implicit conversion FROM the type since
the new struct may have other members that need to be initialized
too.
More information about the Digitalmars-d-learn
mailing list