Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Scala optimize this match to a switch?

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.

like image 280
Maurício Linhares Avatar asked Nov 03 '22 22:11

Maurício Linhares


1 Answers

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).

like image 97
Maurício Linhares Avatar answered Nov 15 '22 07:11

Maurício Linhares