@property - take it behind the woodshed and shoot it?
Rob T
alanb at ucora.com
Sat Jan 26 18:48:35 PST 2013
On Sunday, 27 January 2013 at 01:11:05 UTC, H. S. Teoh wrote:
> On Sun, Jan 27, 2013 at 01:15:29AM +0100, Rob T wrote:
>> We can almost implement properties as a regular struct
> [...]
>
> You do it like this:
>
> import std.stdio;
>
> struct IntProp {
> int __impl;
>
> @property /* <-- ah, the irony! */ int value() {
> return __impl + 123;
> }
> alias value this; // watch this magic
>
> void opAssign(int val) {
> __impl = val - 123;
> }
> }
>
> struct S {
> IntProp prop;
> }
>
> void main() {
> S s;
> writeln(s.prop);
> s.prop = 321;
> writeln(s.prop);
> }
>
>
> T
Ah cool! You don't really need @property however if we adopt the
optional () unless that's to be enforced.
The really nice thing about this, is we can return the struct as
a ref, and it still works, and also take the address of the
struct and it continues to work.
Even better I can add more member functions to it and expand on
what it can do. The "property as a function" approach is far more
limiting and has issues, such as ref returns and taking the
address.
Anyone know what's missing or what won't work with this approach?
--rt
More information about the Digitalmars-d
mailing list