Properties
Nick Sabalausky
a at a.a
Thu Jan 8 14:24:00 PST 2009
"Yigal Chripun" <yigal100 at gmail.com> wrote in message
news:gk5t92$fu2$1 at digitalmars.com...
> 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.
>
*smacks forehead* I completely forget about "outer". That's the only reason
I didn't suggest "this". That is good. But using "new" in that particular
way bothers me a little. What about making the new value a property of
"this":
set { this = this.new; }
set { this = this.next; }
set { this = this.prime; } // Math-style
set { this = this.pending; }
set { this = this.hcwgcwFNbchbSCCGUgucG; }
> 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;
> ...
> }
Good point.
More information about the Digitalmars-d
mailing list