Last active
August 29, 2015 14:10
-
-
Save y-yu/372caed884669e03ae21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Alice { | |
AliceConst; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AliceFactory implements PersonInterface<Alice> { | |
public function new() { } | |
public function make() : Alice { | |
return AAliceConst; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Bob { | |
BobConst; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BobFactory implements PersonInterface<Bob> { | |
public function new() { } | |
public function make() : Bob { | |
return BobConst; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Person<X> { | |
A(x : Alice) : Person<Alice>; | |
B(x : Bob) : Person<Bob>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface PersonInterface<X> { | |
public function make() : AbsPerson<X>; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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