Skip to content

Instantly share code, notes, and snippets.

@YellowAfterlife
Last active August 29, 2015 13:57
Show Gist options
  • Save YellowAfterlife/9536687 to your computer and use it in GitHub Desktop.
Save YellowAfterlife/9536687 to your computer and use it in GitHub Desktop.
Fluent (?) interface trick for Haxe.
using Main.Smartxin;
class A {
public var width:Int;
public function new() { }
}
class B extends A {
public var height:Int;
public function new() super();
}
class Main {
static function main() {
var b = new B();
inline function traceB() trace(b.width + "x" + b.height);
//
b.setWidth(4).setHeight(8);
traceB();
b.setSize(5, 3);
traceB();
b.setSize2(7, 8);
traceB();
}
}
class Smartxin {
public static function setWidth<T:A>(self:T, value:Int):T {
self.width = value;
return self;
}
public static function setHeight<T:B>(self:T, value:Int):T {
self.height = value;
return self;
}
public static function setSize<T:B>(self:T, width:Int, height:Int):T {
self.width = width;
self.height = height;
return self;
}
public static function setSize2<T:B>(self:T, width:Int, height:Int):T {
return self.setWidth(width).setHeight(height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment