alias this doesn't work for properties.
John Colvin
john.loughran.colvin at gmail.com
Thu Aug 1 16:51:37 PDT 2013
On Thursday, 1 August 2013 at 22:23:40 UTC, JS wrote:
> This is wrong because
>
> (cast(A)b.Y) = 3;
>
> works.
That's an interesting case. cast(A)b.Y) is using the B getter
property, which makes sense as, due to the cast, nothing in B can
be called for the assigment.
So you have (in psuedo-code, all properties expanded as
getY/setY):
b.getY.castToA = 3;
wheras the previous example is:
b.setY(3);
Neither are an int being implicitly converted to an A
> b.Y = 3 should do the same as b.a = 3 which should be the same
> as b.a.x = 3.
Why that can't work:
struct A
{
double x;
alias x this;
}
struct B
{
private A a;
@property A Y() { importantGetPrep(); return a; }
@property void Y(A v) { importantSetPrep(); a = v; }
}
void main()
{
B b;
b.Y = 3; //cannot be a call to the setter as cannot convert
3 to A .
//cannot be a call to the getter followed by an
assigment
//as the assignment would then have no effect on b.a
//due to value semantics.
//cannot be anything else as it would bypass the
//important*etPrep that must be called on all
accesses
//to B.a .
}
Putting functions like important*etPrep in properties are one of
the main reasons why properties are useful, it would be a very
bad idea to let them be circumvented by an implementation change
in an internal struct.
More information about the Digitalmars-d
mailing list