Created
June 12, 2013 08:45
-
-
Save paulp/5763757 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
class Cell[T](value: T) { | |
var other: T = value | |
} | |
object Test { | |
def g[T, U](x: T, y: U) = if (util.Random.nextBoolean) x else y | |
def f1 = g(1f, "abc") | |
def f2 = g(1: Byte, "abc") | |
def f3 = g(f1, f2) | |
/*** Inferred return types: | |
def g[T, U](x: T, y: U): T|U | |
def f1: Float|String | |
def f2: Byte|String | |
def f3: String|Float|Byte | |
***/ | |
def main(args: Array[String]): Unit = { | |
val x = new Cell(f3) | |
x.other = 1f // ok ( Float <:< Byte|Float|String) | |
x.other = (1: Byte) // ok ( Byte <:< Byte|Float|String) | |
x.other = "def" // ok (String <:< Byte|Float|String) | |
x.other = 1d // nope | |
// a.scala:15: error: type mismatch; | |
// found : Double(1.0) | |
// required: Byte|Float|String | |
// x.other = 1d | |
// ^ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment