Properties

Michiel Helvensteijn nomail at please.com
Thu Jan 8 09:01:05 PST 2009


Nick Sabalausky wrote:

> 1. Like in C#, you shouldn't need to define paramater lists for "set" and
> "get". They're always going to be the same. In the case of "set", it's
> always going to be just the one param, and it'll be the new value, so just
> make a special predefined var. Something like:
> 
> get { return this.len; }
> set { this.len = value; } // "value" (like in C#), or "$" or something
> like that

I have to disagree. By that logic, we would abolish parameter-names
altogether and access formal parameters by number. set has a parameter, and
the programmer should be able to name it.

Also, removing the parentheses would be confusing. I believe it might be
better to make them look like this, even:

property int length {
    auto get() { .. }
    void set(auto name) { .. }
}

So they would clearly be function declarations.

> 2. You shouldn't have to manually define a private var to go along with
> the property. In languages with real properties, the following idiom is
> used constantly:
> 
> private int _var;
> public property int var {
>    get { return _var; }
>    set { _var = $; }
>    void opIncrement() { _var++; }
> }
> 
> Why should that be needed? It should be like this:
> 
> public property int var {
>    // int internalValue; // Automatically created (but named better)
>    get { return internalValue; }
>    set { internalValue = $; }
>    void opIncrement() { internalValue++; }
> }
> 
> In the minority of cases where a property doesn't need this variable,
> "internalValue" can just be optimized away.

If you really want that behavior, you should just use a public variable.
Even changing it to a real property later would not matter for the public
interface.

-- 
Michiel




More information about the Digitalmars-d mailing list