DIP4: Properties
Michel Fortin
michel.fortin at michelf.com
Sat Jul 25 06:36:53 PDT 2009
On 2009-07-25 08:07:56 -0400, KennyTM~ <kennytm at gmail.com> said:
> Michel Fortin wrote:
>>
>> In the other thread, I suggested this, which could aleviate the problem:
>>
>> int foo.opGet(); // getter
>> void foo.opAssign(int); // setter
>>
>> with some support from the compiler.
>>
>> It could even be exteded to support more:
>>
>> int foo.opIndex(int); // foo[1];
>> void foo.opAddAssign(int); / foo += 1;
>> void foo.invert(); // special function attached to property
>>
>> Basically, all you need to implement properties is not a dedicaced
>> "property" syntax, it's a syntax to implement some kind of local
>> namespace, and am "opGet" or "opValue" operator for representing the
>> local namespace. It could also be expressed like this:
>>
>> namespace foo {
>> int opGet(); // getter
>> void opAssign(int); // setter
>> ...
>> int opIndex(int); // foo[1];
>> void opAddAssign(int); / foo += 1;
>> void invert(); // special function attached to property
>> }
>>
>> In both cases, the result would be the same:
>>
>> foo = 1; // same as foo.opAssign(1);
>> return foo; // same as return foo.opGet();
>>
>
> If property is going to be extended like this, why not just make a
> nested struct. Works even now.
Close, but a nested struct doesn't have access to the outer class, nor
can you override its functions in a derived class. I'd say what I'm
proposing is closer to a named mixin:
class Z
{
int x;
template Property()
{
void opAssign(int v) { x = v; }
void opAddAssign(int v) { x += v; }
int get() { return x; }
alias get this;
}
mixin Property property;
}
Z z = new Z;
z.property = 1; // works!
What doesn't work here is the "alias get this" line: you're still
forced to explicitly call the getter. And I have the feeling that
overriding in a derived class won't work either.
Oh and the syntax isn't great at all.
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list