alias this doesn't work for properties.
JS
js.mdnq at gmail.com
Thu Aug 1 15:23:39 PDT 2013
On Thursday, 1 August 2013 at 21:56:46 UTC, John Colvin wrote:
> On Thursday, 1 August 2013 at 20:42:08 UTC, JS wrote:
>> http://dpaste.dzfl.pl/0c923861
>>
>> class A
>> {
>> double x;
>> @property double X() { return x; }
>> @property double X(double v) { return x = v; }
>> alias X this;
>> }
>>
>> class B
>> {
>> A a;
>> @property A Y() { return a; }
>> @property A Y(A v) { return a = v; }
>> }
>>
>>
>> void main()
>> {
>> B b = new B();
>> b.Y = 3;
>> }
>>
>> error not a property of b.Y
>>
>>
>>
>> adding refs to the returns of the getters allows the code to
>> work but only getters are called(hence the need for refs).
>>
>> Please allow alias this to work with overloaded functions.
>> (specifically properties, b.Y = 3; is not b.Y() = 3 but
>> b.Y(3);)
>
> b.Y = 3 is rewritten as b.Y(3)
>
> You are asking for an "int" to be be implicitly convertible to
> an "A", which isn't possible.
This is wrong because
(cast(A)b.Y) = 3;
works.
b.Y(3) should work because A is aliased to double.
One of the issues is
b.Y(3)
The setter of B is never called.
You are right that b.Y(3) does not accept an int(or double) BUT
this is a bug in the compiler because it should since 3 can be
assigned to it. It's just that the compiler isn't smart enough to
see this because of the way it calculates the assignment.
b.Y = 3 should do the same as b.a = 3 which should be the same as
b.a.x = 3.
If you think about what the code is actually doing you will see
it should be possible.
http://dpaste.dzfl.pl/ac376ca0
More information about the Digitalmars-d
mailing list