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
Scala emits a switch instruction when matching on Int or String but not when matching on a union of those types.
Compiler version
3.3.3, 3.4.1
Minimized code
defmatchInt(x: Int):String= x match {
case1=>"one"case2=>"two"case3=>"three"case4=>"four"case5=>"five"
}
defmatchIntUnion(x: 1|2|3|4|5):String= x match {
case1=>"one"case2=>"two"case3=>"three"case4=>"four"case5=>"five"
}
defmatchString(s: String):Int= s match {
case"one"=>1case"two"=>2case"three"=>3case"four"=>4case"five"=>5
}
defmatchStringUnion(s: "one"|"two"|"three"|"four"|"five"):Int= s match {
case"one"=>1case"two"=>2case"three"=>3case"four"=>4case"five"=>5
}
Uh oh!
There was an error while loading. Please reload this page.
Scala emits a switch instruction when matching on
Int
orString
but not when matching on a union of those types.Compiler version
3.3.3, 3.4.1
Minimized code
Output
The above code (de)compiles to:
Expectation
Emit a switch instruction when all members of the union share a base type with a switch-compatible type. For example:
1 | 2 | 3 | 4 | 5
can emit a switch because the union widens toInt
"one" | "two" | "three" | "four" | "five"
can emit a switch because the union widens toString
'a' | 'b' | 'c'
can emit a switch because the union widens toChar
1 | 2 | 3 | "four" | "five"
cannot emit a switchByte
orShort
can also be used in a switch but there's no syntax to make literals for those types.The text was updated successfully, but these errors were encountered: