Skip to content

Instantly share code, notes, and snippets.

@johnynek
Last active August 29, 2015 14:11
Show Gist options
  • Save johnynek/8d315ea291bb1d553e9a to your computer and use it in GitHub Desktop.
Save johnynek/8d315ea291bb1d553e9a to your computer and use it in GitHub Desktop.
type inference with cases. Why does this fail in scala 2.10.4? Is my logic wrong, or is the compiler not smart enough?
sealed trait Key {
type Inner
}
trait IntKey extends Key {
type Inner = Int
}
trait StringKey extends Key {
type Inner = String
}
// This should compile
object Key {
def work(m: Key): m.Inner = m match {
case i: IntKey => 1 //m in IntKey, and IntKey.Inner is Int, so we are good, right?
case s: StringKey => "1"
}
}
/* in scala 2.10.4 I see this:
test.scala:28: error: type mismatch;
found : Int(1)
required: m.Inner
case i: IntKey => 1
^
test.scala:29: error: type mismatch;
found : String("1")
required: m.Inner
case s: StringKey => "1"
*/
@adriaanm
Copy link

I agree completely, and expect this will be improved in 3.0.

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