Properties
Yigal Chripun
yigal100 at gmail.com
Thu Jan 8 14:03:40 PST 2009
Nick Sabalausky wrote:
> "bearophile"<bearophileHUGS at lycos.com> wrote in message
> news:gk5grh$2o8i$1 at digitalmars.com...
>> Chad J:
>>> public property int var
>>> {
>>> get { return var; }
>>> set { var = $; }
>>> }
>>>
>>> public property int foo
>>> {
>>> get { return foo; }
>>> set { foo = $; }
>>> }
>> I think I have suggested something similar, time ago.
>>
>> The default situations you have shown may enjoy an even shorter syntax,
>> for example:
>> public property int var { get set }
>>
>> That can also become the following when you want only a getter or setter:
>> public property int var { get }
>> Or:
>> public property int var { set }
>>
>>
>> (I don't like all those repeated "foo"/"var", it's not much DRY. But at
>> the moment I see no better solution, that works well for nested
>> classes/structs too).
>>
>
> Something like this:
>
> public property int var
> {
> get { return property; }
> set { property = $; }
> }
>
> Or something like this:
>
> public property int foo
> {
> get { return $; }
> set { $ = $new; }
> }
>
>> Let's see if Walter likes all this.
>>
>> Bye,
>> bearophile
>
>
I like the general idea, but why invent new words?
how about this:
public property int var {
get { return this; }
set { this = new; }
}
there's one issue with the above - what about the "this" of the
container class/struct, but that's solved easily with "outer" just like
in nested classes.
it makes sense to me since the feature seems to be mainly syntax sugar
for something like the following:
class A {
struct Prop {
int internal;
int opCall() { return internal; }
void opAssign(int value) { internal = value; }
}
public Prop prop;
...
}
void main() {
auto a = new A;
int num = a.prop;
a.prop = 9;
...
}
More information about the Digitalmars-d
mailing list