DIP4: Properties

Daniel Keep daniel.keep.lists at gmail.com
Sat Jul 25 02:43:17 PDT 2009


Andrei and Rainer have come up with an alternative that might work.
Addressing the points in the Rationale section:

* It may or may prevent unintended usage depending on how it's
implemented, but will certainly CONVEY the expected usage.

* As above, it may or may not solve the ambiguous code problem depending
on implementation details.

* +=, -=, etc.: I've said before that this should be addressed separately.

* It makes properties discoverable.

* There is a solution to the DRY problem.

Basically, the idea is that this:

interface I
{
    int foo();
    int foo(int);
}

becomes:

interface I
{
    int opGet_foo();
    int opSet_foo(int);
}

As I said, this addresses most of the problems: IDEs and debuggers can
simply look for opGet/opSet pairs.  It's extensible since we can add
more "operators" in the future, AND it plays nicely with inheritance.

The problem is that it still violates DRY.  However, I knocked together
a basic prototype property generator that helps alleviate this:

http://gist.github.com/154755

Possible implementations of the above:

class A : I
{
    // Automatic getter, setter and storage
    mixin( Property!(int, "foo") );
}

class B : I
{
    // Fully custom with custom storage and protection
    mixin( Property!(int, "foo",
        q{
            return storage*100;
        },
        q{
            return storage=value/100;
        }
    ));
}

With a more sophisticated template, it could probably be simplified even
further.

The nice thing about this is that it accomplishes the goals of the DIP
without introducing ANY new syntax.  It does still require compiler
changes to map a.b to a.opGet_b and a.b=c to a.opSet_b(c), but it would
hopefully be more palatable to Walter.

If you want, I can write this up as an alternative solution to the DIP.

Rainer's post: news://news.digitalmars.com:119/h4dpoq$2hpu$1@digitalmars.com
Andrei's post: news://news.digitalmars.com:119/h4dscd$2n7f$1@digitalmars.com
My response to Andrei's post:
news://news.digitalmars.com:119/h4ej0g$11p7$1@digitalmars.com



More information about the Digitalmars-d mailing list