DIP23 draft: Fixing properties redux

Jonathan M Davis jmdavisProg at gmx.com
Sun Feb 3 20:38:29 PST 2013


On Sunday, February 03, 2013 03:16:08 Andrei Alexandrescu wrote:
> Destroy.

Another thing to consider would be to allow putting @property on a variable 
with the idea that variables marked as such would not be usable in any 
situation where a property function wouldn't be - such as taking their address 
or passing them by ref. That would reduce code breakage when switching between 
public variables and property functions.

The alternative would be to make it so that putting @property on a variable 
would lower it to property functions. So,

struct S
{
    @property int prop;
}

becomes something like

struct S
{
    private int __prop;

    @property int prop() @safe inout pure nothrow
    { return __prop; }

    @property int prop(int value) @safe pure nothrow
    { return __prop = value; }
}

One of the main ideas behind properties is to make it so that you can make 
something a variable when you don't the extra boilerplate for setter and 
getter functions (e.g. because you don't need to check the value it's being 
set to), but it's still easy to swap it out with an actual getter and setter 
function later when you actually _do_ need extra stuff in those functions. And 
if we don't do something to indicate that a variable could be swapped out with 
a property function later, then you risk code doing stuff like taking its 
address or passing it by ref, and that code will then break if the variable 
ever does become a property function.

- Jonathan M Davis


More information about the Digitalmars-d mailing list