implicit conversion to alias this

BLM768 blm768 at gmail.com
Wed Jun 27 21:43:06 PDT 2012


On Monday, 25 June 2012 at 20:06:21 UTC, Tobias Pankrath wrote:
> ---------
> struct A { bool a; alias a this; }
> struct B { int b; alias b this; }
>
> A a = false; // works
> B b = 12; // works
>
> struct C
> {
>     A aa;
>     B ab;
> }
>
> C c = { false, 12 }; // does not work, because the implicit 
> conversion does not happen.
> -----
>
> What is the reason to allow the first two assignments? Isn't it 
> just another implicit conversion that shouldn't be allowed?

The compiler isn't "smart" enough to realize that you're trying 
to use an implicit conversion. It's only told to expect the 
conversion in certain situations, such as assignment, and getting 
it to work for rarer cases would involve patching it with more 
special-case code.  The initializer is also expecting struct 
_values_; I don't think that you could call a function that wants 
an A and give it a bool, either. Making a struct value from an 
expression and initializing a struct in a definition are subtly 
different and follow different codepaths in the compiler.


More information about the Digitalmars-d-learn mailing list