alias this doesn't work for properties.

John Colvin john.loughran.colvin at gmail.com
Fri Aug 2 05:15:18 PDT 2013


On Friday, 2 August 2013 at 02:21:25 UTC, Maxim Fomin wrote:
> On Thursday, 1 August 2013 at 21:56:46 UTC, John Colvin wrote:
>>
>> You are asking for an "int" to be be implicitly convertable to 
>> an "A", which isn't possible.
>
> http://dpaste.dzfl.pl/c3cce6e2
>
> import std.stdio;
>
>
> class A
> {
>     double x;
>     @property double X() { writeln("From A.Get"); return x; }
>     @property double X(double v) { writeln("From A.Set"); x = 
> v; return x; }
>     alias X this;
> 	this(double val) { this.x = val; }
> }
>
> class B
> {
> 	A a;
>     A Y() { writeln("From B.Get"); return a; }
>     A Y(A v ...) { writeln("From B.Set"); a =  v; return a; }
>
> 	this() { a = new A(0); }
> }
>
>
> void main()
> {
> 	B b = new B();
> 	writeln(b.Y);
> 	b.Y = 3;
> 	writeln(b.Y);
>
> 	readln();
> }

Ok... It doesn't work for structs though which is strange. The 
docs do only mention classes however.

That's not really an implicit conversion, it's an implicit 
contstruction. It's definitely not like alias this. (In your 
example the alias this is completely unused)

Does this have any bearing on the wider question? The variadic 
class construction syntax could be allowed for properties, with a 
restriction to only one argument passed.


More information about the Digitalmars-d mailing list