You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
deff1(l: Array[Int]):Array[Any] =
l.map{ x =>
x match {
case x if x ==1=> x.toString
case x if x ==2=> x.toInt
case _ => x.toDouble
}
}
f1(Array(1,2,3)).foreach(v => println(s"$v :${v.getClass}"))
1:classjava.lang.String2.0:classjava.lang.Double3.0:classjava.lang.Double
The text was updated successfully, but these errors were encountered:
because of the weak-least-upper-bound rule in SLS 8.4.
Regardless, the behavior in your example does seem incorrect to me. Looks like a bug in weak-least-upper-bound handling in the pattern matcher. The weak-least-upper-bound of Int, Double, and String is Any; I don't see anything in the spec that would permit the weak-least-upper-bound of only a subset of the clauses to influence the results.
Note that the behavior is sensitive to the ordering of clauses in the match, so for example you get different results if String comes last:
l.map{
case x if x ==1=> x.toInt
case x if x ==2=> x.toDouble
case x => x.toString
}
1:classjava.lang.Integer2.0:classjava.lang.Double3:classjava.lang.String
The second result type should be Int
The text was updated successfully, but these errors were encountered: