Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Created January 18, 2015 16:16
Show Gist options
  • Save Blaisorblade/dae225ca756ad4e5c5e6 to your computer and use it in GitHub Desktop.
Save Blaisorblade/dae225ca756ad4e5c5e6 to your computer and use it in GitHub Desktop.
Try encoding SML functors in Scala, v2
// 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]
@Blaisorblade
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment