On 07/20/2014 11:14 AM, Eric wrote:
>
>> Use @property when you want a pseudo-variable or something that might
>> be conceptually considered a "property" of the object, i.e. to do this:
>>
>> auto blah = thing.someProperty;
>> thing.someProperty = blahblah;
>
> This is basically what I suspected.  But why
> write:
>
> @property int getValue() { return(value); }
>
> When you could just have a public field:
>
> int value;
>
> That lets you set and get the value without the parens anyways?

Freely setting a member makes sense only in limited cases where that member does not take any part in any invariant of the object. For example, if a Rectangle class has .length, .width, and .area, it would be an error to set either of them.

Additionally, properties enable one to make it look like a type has such a member:

struct Rectangle
{
    // ...

    @property int area()
    {
        return length * width;
    }
}

Ali

Reply via email to