Created
January 18, 2015 16:16
-
-
Save Blaisorblade/dae225ca756ad4e5c5e6 to your computer and use it in GitHub Desktop.
Try encoding SML functors in Scala, v2
This file contains hidden or 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
// A variant of https://gist.github.com/Blaisorblade/19ef1abb28e20995e187, answering | |
// http://stackoverflow.com/a/23019436/53974 | |
trait ORD { | |
type T | |
def leq(a: T, b: T): Boolean | |
} | |
// The most direct attempt runs into limitations of refinements. | |
//def F(X : ORD) = new { def eq(x : X.T, y : X.T) = X.leq(x, y) && X.leq(y, x) } | |
// Let's avoid refinements instead by declaring the signature we want — this time without the T member, like in the original | |
trait EQ[T] { | |
def eq(x : T, y : T): Boolean | |
} | |
def F(X : ORD) = new EQ[X.T] { def eq(x : X.T, y : X.T) = X.leq(x, y) && X.leq(y, x) } | |
//F: (X: ORD)EQ[X.T] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are the links again, in clickable form. This is a variant of https://gist.github.com/Blaisorblade/19ef1abb28e20995e187, answering http://stackoverflow.com/a/23019436/53974.