implicit conversion to alias this

Tobias Pankrath tobias at pankrath.net
Wed Jun 27 22:23:55 PDT 2012


On Thursday, 28 June 2012 at 04:43:07 UTC, BLM768 wrote:
> 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.

I'm fine that the assignment to C is verboten. I'd disallow the 
first assignments to and would like to know, why they are kept.



More information about the Digitalmars-d-learn mailing list