Last active
August 29, 2015 14:11
-
-
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?
This file contains hidden or 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
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" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I agree completely, and expect this will be improved in 3.0.