为什么scala不能将Long switch语句编译为tableswitch

Car*_*cas 2 scala switch-statement

当我做

val oldId :Long  = 123;

val i = 1
val newId = (oldId: @switch) match {
  case 1  => 1234
  case 2  => 5678
  case 3  => 1122
  case 4  => 3344
}
Run Code Online (Sandbox Code Playgroud)

我得到编译器警告

[ant:scalac] mycode.scala:25: warning: could not emit switch for @switch annotated match
[ant:scalac]     val newId = (oldId: @switch) match {
[ant:scalac]                          ^
[ant:scalac] one warning found
Run Code Online (Sandbox Code Playgroud)

但是,如果我改为使用以下代码

val oldId :Int  = 123;

val i = 1
val newId = (oldId: @switch) match {
  case 1  => 1234
  case 2  => 5678
  case 3  => 1122
  case 4  => 3344
}
Run Code Online (Sandbox Code Playgroud)

然后编译器没有给我一个警告.为什么不能使用a Long并得到一个tableswitch?

Yuv*_*kov 5

为什么不能使用Long并获得一个桌面开关?

因为JVM 支持@tableswitch int并且类型可转换为int:

Java虚拟机的tableswitch和lookupswitch指令仅对int数据有效.因为对byte,char或short值的操作在内部提升为int,所以编译一个表达式求值为其中一个类型的开关,就好像它计算为int类型一样

因此,您会收到编译时警告,因为long它不能隐式转换为int.