ReturnThis/@chain
Lutger
lutger.blijdestijn at gmail.com
Sat Jul 24 03:15:51 PDT 2010
dsimcha wrote:
> 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.
Why not use covariance? Or is that too much boilerplate?
this will work:
class Bar : Foo {
override typeof(this) val(Type newVal) {
return super(newVal);
}
}
This could be automated with a mixin.
More information about the Digitalmars-d
mailing list