Why can't Scala optimize this match to a switch?
constants, optimization, pattern-matching, scala, switch-statement
Solution
The answer as provided at the scala-user forum was making the vals final and removing the `Byte` annotation:
object Message {
final val Authentication = 'R'
final val BackendKeyData = 'K'
final val Bind = 'B'
final val BindComplete = '2'
}
Now the `@switch` correctly generates a `lookupswitch` (it doesn't generate a `tableswitch` but it's good enough).
Problem
I have this pattern match that matches only on Byte values but when I add the `@switch` it says: could not emit switch to @switch annotated match What am I missing here? Just FYI, what I have tried already and did not work: - Moving this constant to a Java interface and using `public static final byte` fields (I have also tried making them `int` instead of `byte`) - Marking the fields as `final val` at the Scala `Message` companion object - Marking the fields as `@inline` at the companion object I'm definitely lost here.