ReturnThis/@chain

dsimcha dsimcha at yahoo.com
Fri Jul 23 19:21:57 PDT 2010


I'm working on the next iteration of my Plot2Kill library, and I **really**
have fallen in love with property chaining.  It's the greatest thing since
sliced arrays.  However, I've run into an issue that I had previously overlooked:

class Foo {
    Type _val;

    typeof(this) val(Type newVal) {
        _val = newVal;
        return this;
    }
}

class Bar : Foo {
    // More properties.
}

void main() {
    auto bar = new Bar;
    bar.val(someValue).barProperty(someOtherValue);  // Won't work.
}

Given that D2 is mostly finalized, it may be a little late for this, but would
it be feasible, at least far down the road, to add something like an @chain
annotation, which would only allow a member function to return this and would
implicitly downcast it to the subtype that was passed in as the this pointer?
 For example, assume we attached @chain to Foo.val():

pragma(msg, typeof(bar.val(someValue)));  // Bar, not Foo.


More information about the Digitalmars-d mailing list