Last active
August 1, 2017 18:27
-
-
Save frabbit/bae4b03d255ef8f4393ba70e731264ab 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
package; | |
import scuts.implicit.Wildcard; | |
import scuts.implicit.Implicit; | |
import scuts.implicit.Instances; | |
import Main.BoolInstance.instance; | |
// for nicer syntax | |
using Main.EqSyntax; | |
using Main.OrdSyntax; | |
interface Eq<T> { | |
public function eq (a:T, b:T):Bool; | |
} | |
interface Ord<T> extends Eq<T> { | |
public function compare (a:T, b:T):Int; | |
} | |
class BoolInstance | |
implements Eq<Bool> | |
implements Ord<Bool> | |
{ | |
@:implicit public static var instance:Instances<Eq<Bool>, Ord<Bool>> = new BoolInstance(); | |
public function new () {} | |
public function eq (a:Bool, b:Bool):Bool { | |
return a == b; | |
} | |
public function compare (a:Bool, b:Bool):Int { | |
return switch [a,b] { | |
case [true, false]: 1; | |
case [false, true]: -1; | |
case _: 0; | |
} | |
} | |
} | |
class EqSyntax { | |
public static inline function eq <T>(a:T, b:T, eq:Implicit<Eq<T>>):Bool { | |
return eq.eq(a,b); | |
} | |
} | |
class OrdSyntax { | |
public static inline function compare <T>(a:T, b:T, o:Implicit<Ord<T>>):Int { | |
return o.compare(a,b); | |
} | |
} | |
class Main { | |
static function main () { | |
trace(true.eq(false,_)); // _ means implicit injection | |
trace(true.compare(false,_)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment