digitalmars.D.learn - post/pre-increment/decrement and property
- Vidar Wahlberg (17/17) Feb 07 2012 Take the following code:
- Robert Clipsham (19/36) Feb 07 2012 Try this:
- Timon Gehr (2/38) Feb 07 2012 Yes, but then he cannot verify the new value.
- Robert Clipsham (8/26) Feb 07 2012 So what's actually being asked is can the following happen then?
- Jacob Carlborg (4/28) Feb 07 2012 Yes, we need some form of property rewrite. Wasn't someone working on th...
Take the following code: int _foo; property auto foo() { return _foo; } property auto foo(int foo) { return _foo = foo; } void main() { ++foo; } This won't compile, and it sort of makes sense (at least to me), but is it (or will it in the future be) possible to achieve this in some way? I like to encapsulate class/struct members this way so I can easily add validation of the value in the setter at a later time (granted, I can add getter/setter properties when it turns out that I do need to validate the values, but that's beside the point).
Feb 07 2012
On 07/02/2012 22:37, Vidar Wahlberg wrote:Take the following code: int _foo; property auto foo() { return _foo; } property auto foo(int foo) { return _foo = foo; } void main() { ++foo; } This won't compile, and it sort of makes sense (at least to me), but is it (or will it in the future be) possible to achieve this in some way? I like to encapsulate class/struct members this way so I can easily add validation of the value in the setter at a later time (granted, I can add getter/setter properties when it turns out that I do need to validate the values, but that's beside the point).Try this: ---- int _foo; property ref foo() { return _foo; } property ref foo(int foo) { return _foo = foo; } void main() { ++foo; } ---- Using 'ref' instead of auto returns a reference to _foo, allowing it to be modified. -- Robert http://octarineparrot.com/
Feb 07 2012
On 02/07/2012 11:54 PM, Robert Clipsham wrote:On 07/02/2012 22:37, Vidar Wahlberg wrote:Yes, but then he cannot verify the new value.Take the following code: int _foo; property auto foo() { return _foo; } property auto foo(int foo) { return _foo = foo; } void main() { ++foo; } This won't compile, and it sort of makes sense (at least to me), but is it (or will it in the future be) possible to achieve this in some way? I like to encapsulate class/struct members this way so I can easily add validation of the value in the setter at a later time (granted, I can add getter/setter properties when it turns out that I do need to validate the values, but that's beside the point).Try this: ---- int _foo; property ref foo() { return _foo; } property ref foo(int foo) { return _foo = foo; } void main() { ++foo; } ---- Using 'ref' instead of auto returns a reference to _foo, allowing it to be modified.
Feb 07 2012
On 07/02/2012 23:04, Timon Gehr wrote:So what's actually being asked is can the following happen then? ++foo; becomes: foo(foo + 1); -- Robert http://octarineparrot.com/Try this: ---- int _foo; property ref foo() { return _foo; } property ref foo(int foo) { return _foo = foo; } void main() { ++foo; } ---- Using 'ref' instead of auto returns a reference to _foo, allowing it to be modified.Yes, but then he cannot verify the new value.
Feb 07 2012
On 2012-02-08 01:50, Robert Clipsham wrote:On 07/02/2012 23:04, Timon Gehr wrote:Yes, we need some form of property rewrite. Wasn't someone working on that? -- /Jacob CarlborgSo what's actually being asked is can the following happen then? ++foo; becomes: foo(foo + 1);Try this: ---- int _foo; property ref foo() { return _foo; } property ref foo(int foo) { return _foo = foo; } void main() { ++foo; } ---- Using 'ref' instead of auto returns a reference to _foo, allowing it to be modified.Yes, but then he cannot verify the new value.
Feb 07 2012