DIP23 draft: Fixing properties redux
Steven Schveighoffer
schveiguy at yahoo.com
Sun Feb 3 04:16:17 PST 2013
On Sun, 03 Feb 2013 03:16:08 -0500, Andrei Alexandrescu
<SeeWebsiteForEmail at erdani.org> wrote:
> Walter and I have had a discussion on how to finalize properties.
>
> http://wiki.dlang.org/DIP23
I agree with everything in this. This looks exactly like what I wanted a
few days ago. Thank you!
One aspect not addressed is free-function properties:
@property int foo(int x) {return x * 5;}
could be interpreted as:
foo = 2; // not correct (for this example)
int x = 2.foo; // correct
Note that:
@property int foo();
@property void foo(int x, int y);
are both unambiguous.
==================
I have a possible suggestion to fix this within your proposal:
@property on single-arg free functions ONLY enables a setter mode, it does
not work as a UFCS getter.
Therefore, if you wish to write a UFCS getter, omit the @property
designation.
int foo(int x) {return x * 5;}
foo = 2; // illegal
int x = 2.foo; // OK, sets x to 10
int x = foo(2); // same as above
@property int foo(int x) {_foo = x;}
foo = 2; // OK, sets _foo to 2
int x = 2.foo; // illegal
int x = foo(2); // illegal
I know this is not a complete solution, and can be confusing, but we have
little options at this point, given existing code. Also note that we
already have a way to specify a getter property on a user-defined type,
UFCS isn't entirely necessary for that.
This will break SOME declarations, but removing @property should result in
compiling code I think.
-Steve
More information about the Digitalmars-d
mailing list