Created
January 2, 2014 17:59
-
-
Save tpolecat/8223293 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
scala> class Foo(val n: Int) | |
defined class Foo | |
scala> class Bar(n: Int) extends Foo(n) | |
defined class Bar | |
scala> List[Foo]().sorted | |
<console>:9: error: No implicit Ordering defined for Foo. | |
List[Foo]().sorted | |
^ | |
scala> implicit val ordFoo = Ordering.by[Foo,Int](_.n) | |
ordFoo: scala.math.Ordering[Foo] = scala.math.Ordering$$anon$9@15fa2b3e | |
scala> List[Foo]().sorted | |
res1: List[Foo] = List() | |
scala> List[Bar]().sorted | |
<console>:11: error: No implicit Ordering defined for Bar. | |
List[Bar]().sorted | |
^ | |
scala> implicit def ordFoo[A <: Foo] = Ordering.by[A,Int](_.n) | |
ordFoo: [A <: Foo]=> scala.math.Ordering[A] | |
scala> List[Bar]().sorted | |
res3: List[Bar] = List() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment