Skip to content

Instantly share code, notes, and snippets.

@paulp
Created June 12, 2013 08:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulp/5763757 to your computer and use it in GitHub Desktop.
Save paulp/5763757 to your computer and use it in GitHub Desktop.
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