@property

Mike James foo at bar.com
Sat Jun 26 03:09:28 PDT 2010


"Pelle" <pelle.mansson at gmail.com> wrote in message 
news:i00ejm$2kii$1 at digitalmars.com...
> As heard around these parts, a lot of people want property-style function 
> calls to require the function to be declared with @property, like this:
>
> @property foo(); //getter
> @property foo(int); //setter
>
> foo; //getter
> foo = 13; //setter
>
> While this seems quite reasonable, in practice I and others feel this 
> leads to confusion, especially the getter part. Mostly when the getter has 
> no setter counterpart. D also lets us call no-argument functions without 
> parentheses today, so for this to happen a lot of code needs to change.
>
> My suggestion is as follows; require @property for single-argument setters 
> *only*. Make the silly writeln = 13; go away, but keep the "a b c".split;. 
> This way, there can be no confusion about @property, and most code will go 
> unchanged.
>
> I hope this was not too late a suggestion. :)

Why not extend @property further (a la objectpascal)...

@property T PropertyName @get _propval @set _propval;

would generate the equivalent of:

T _propval;

@property {
    T PropertyName() { return _propval; }
    T PropertyName(T aval) { return _propval = aval; }
}

or e.g.

@property T PropertyName @get _propval @set MySetValFunc;

and provide the required setter function:

T MySetValFunc(T myVal) {
    _propval = myVal;
    // do something with _propval
    return _propval;
}


-=mike=-




More information about the Digitalmars-d mailing list