chaining
BCS
ao at pathlink.com
Wed Feb 25 14:08:43 PST 2009
Reply to Brian,
> I want to use a chaining system for easy setting of object attributes,
> which would work great for a single object, unfortunately derived
> classes cannot inherit the chained functions implicitly, whats the
> best way around this?
>
> class Base {
> int x;
> Base foo(int x_) {
> this.x = x_;
> return this;
> }
> }
> class Derived : Base {
> Derived bar(int y_) {
> return this;
> }
> }
> // usage:
> auto m = (new Derived).foo(10).bar(x); // bar can be accessed through
> foo
mixins:
template BaseT(){
typeof(this) foo()(int x_) {
this.x = x_;
return this;
}
}
class Base {
int x;
mixin BaseT!();
}
template DerivedT()
{
typeof(this) bar(int y_) {
return this;
}
mixin BaseT!();
}
class Derived : Base {
mixin DerivedT!();
}
void main()
{
int x;
// usage:
auto m = (new Derived).foo(10).bar(x); // bar can be accessed through foo
}
More information about the Digitalmars-d-learn
mailing list