Skip to content

Instantly share code, notes, and snippets.

@tpolecat
Created January 2, 2014 17:59
Show Gist options
  • Save tpolecat/8223293 to your computer and use it in GitHub Desktop.
Save tpolecat/8223293 to your computer and use it in GitHub Desktop.
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