I find this behavior surprising and wonder if it is according to spec:
$ scala-cli -S 3.nightly
Welcome to Scala 3.4.1-RC1-bin-20240207-551eae4-NIGHTLY-git-551eae4 (17.0.10, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
scala> var v1: 1 | 2 = 1
var v1: 1 | 2 = 1
scala> val v2: 1 | 2 = 1
val v2: 1 | 2 = 1
scala> val p = (42, 43)
val p: (Int, Int) = (42,43)
scala> p(v1)
val res0: Any = 43
scala> p(1)
val res1: Int = 43
scala> p(v2)
val res2:
v2.type match {
case 0 => Int
case scala.compiletime.ops.int.S[n1] =>
Tuple.Elem[Int *: EmptyTuple.type, n1]
} = 43
I would expect this:
not-current-scala> p(v1)
val res0: Int = 43
not-current-scala> p(v2)
val res3: Int = 43
There are several things at play here: The apply method on tuples, union types and the difference in behavior of var/val although same type.
Can anyone explain what happens and tell if providing better inference is feasible/desirable/according to spec?