Skip to content

Instantly share code, notes, and snippets.

@y-yu
Last active August 29, 2015 14:10
Show Gist options
  • Save y-yu/372caed884669e03ae21 to your computer and use it in GitHub Desktop.
Save y-yu/372caed884669e03ae21 to your computer and use it in GitHub Desktop.
abstract AbsPerson<X>(Person<X>) {
public function new(x) {
this = x;
}
@:from public static function fromAlice(a : Alice) : AbsPerson<Alice> {
return new AbsPerson(A(a));
}
@:from public static function fromBob(b : Bob) : AbsPerson<Bob> {
return new AbsPerson(B(b));
}
@:from public static function fromPerson<T>(x : Person<T>) : AbsPerson<T> {
return new AbsPerson(x);
}
@:to public function toX() : X {
return switch(this) {
case A(a): a;
case B(b): b;
}
}
@:to public function toAbsPerson() : AbsPerson<X> {
return this;
}
}
enum Alice {
AliceConst;
}
class AliceFactory implements PersonInterface<Alice> {
public function new() { }
public function make() : Alice {
return AAliceConst;
}
}
enum Bob {
BobConst;
}
class BobFactory implements PersonInterface<Bob> {
public function new() { }
public function make() : Bob {
return BobConst;
}
}
enum Person<X> {
A(x : Alice) : Person<Alice>;
B(x : Bob) : Person<Bob>;
}
interface PersonInterface<X> {
public function make() : AbsPerson<X>;
}
class Test {
static function test<T>(m : PersonInterface<T>) : T {
return m.make();
}
static function main() {
var x = test(new AliceFactory());
$type(x);
trace(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment