ReturnThis/@chain

Eric Poggel dnewsgroup at yage3d.net
Fri Jul 23 20:41:17 PDT 2010


On 7/23/2010 10:21 PM, dsimcha wrote:
> 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.
> }

What about using templates?  I haven't tested this code but it seems 
like I've accomplished something similar before.

class Foo(T=Foo) {
     Type _val;

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

class Bar : Foo!(Bar) {
     // More properties.
}


More information about the Digitalmars-d mailing list