Implicit conversion in constructor

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 19:28:08 PDT 2015


On Saturday, 18 July 2015 at 02:23:26 UTC, rcorre wrote:
> Is there any reason why implicit conversion from Foo to Thing 
> is permitted in a regular method but not in a constructor?
>
> Trying to figure out whether this is a bug or some sort of 
> constructor-specific safety precaution.
>
> struct Thing {
>   Foo foo;
>   alias foo this;
> }
>
> class Foo { }
>
> class Bar {
>   Thing thing;
>
>   void fine() {
>     thing = new Foo(); // ok
>   }
>
>   this() {
>     thing = new Foo(); // nope!
>   }
> }

I even am not sure how in the world it allows implicit conversion 
from class to struct in "fine" at all.


More information about the Digitalmars-d-learn mailing list