Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save travisbrown/cdd878f7739ab34ce079 to your computer and use it in GitHub Desktop.
Save travisbrown/cdd878f7739ab34ce079 to your computer and use it in GitHub Desktop.
sealed trait Key {
type Inner
def inner: Inner
}
trait IntKey extends Key {
type Inner = Int
}
trait StringKey extends Key {
type Inner = String
}
object Key {
def work(m: Key): m.Inner = m match {
case i: IntKey => i.inner
case s: StringKey => s.inner
}
}
@travisbrown
Copy link
Author

Pretty neat compiler error message:

<console>:12: error: type mismatch;
 found   : i.scala.Inner
    (which expands to)  Int
 required: m.Inner
          case i: IntKey => i.inner
                              ^
<console>:13: error: type mismatch;
 found   : s.java.lang.Inner
    (which expands to)  String
 required: m.Inner
          case s: StringKey => s.inner
                                 ^

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