Last active
December 31, 2015 11:29
-
-
Save frabbit/7979748 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
interface Filterable<M, A> { | |
public function filter (f:A->Bool):Of<M,A>; | |
} | |
class MyArray<A> implements Filterable<MyArray<In>, A> { | |
var a:Array<A>; | |
public function new (?a:Array<A>) { | |
this.a = a == null ? [] else a; | |
} | |
public function filter(f:A->Bool):MyArray<A> { | |
return new MyArray(a.filter(f)); | |
} | |
} | |
class MyList<A> implements Filterable<MyList<In>, A> | |
{ | |
var a:List<A>; | |
public function new () { | |
this.a = a == null ? new List() else a; | |
} | |
public function filter(f:A->Bool):MyList<A> { | |
return new MyList(a.filter(f)); | |
} | |
} | |
class Main { | |
// abstract over the type constructor | |
public static function withFilterable <M>(m:Filterable<M, Int>):Of<M,Int> | |
{ | |
return m.filter(function (x) return x > 10); | |
} | |
public static function main () { | |
trace(withFilterable(new MyArray())); // the result is typed as MyArray<Int> | |
trace(withFilterable(new MyList())); // the result is typed as MyList<Int> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment