Scala 3 pattern matching on `opaque type` is not tableSwitch

object Foos {
  opaque type Foo = Int 
  // type Foo  = Int ;//does not work too
  
  final val A:Foo = 1
  final val B:Foo = 2
  final val C:Foo = 3
  final val D:Foo =4
}

import Foos.Foo

def test(x: Foo): Int = (x: @scala.annotation.switch) match {
  case Foos.A => 1
  case Foos.B => 2
  case Foos.C => 3
  case Foos.D => 4
}

which gives me Could not emit switch for @switch annotated match

Seems it can’t be a table switch ?

1 Like

If you add a type ascription Foo to A, then the compiler loses the information that it’s 1. It’s not necessarily because of the opaque type; you get the same issue if you use Int everywhere. It’s just that the opaque type makes the problem unavoidable if you want to hide the relationship between Foo and Int completely. Only if the opaque type is transparent can you make it work.

2 Likes

Thanks but the problem is when once I remove the :Foo then I can’t pass it to where a Foo is needed.

Required: ~~ndition.optimizer.CompOps.CompOp
~~ substitute/GroupingVarSubstitution.scala:66:14: Found:    (~~ondition.optimizer.CompOps.In : (7 : Int))