Skip to content

Pattern Match with Default Case Cannot Be Optimized To Switch Statement #9776

Closed
@adamgfraser

Description

@adamgfraser

Minimized code

import scala.annotation.switch

sealed trait Fruit

object Fruit {

  case object Apple extends Fruit
  case object Banana extends Fruit
  case object Orange extends Fruit
  
  def isCitrus(fruit: Fruit): Boolean =
    (fruit: @switch) match {
      case Orange => true
      case _      => false
    }
}

Output

// Could not emit switch for @switch annotated match since there are not enough cases

Expectation

I would have expected this to work as it appears to on Scala 2. I can understand that there are only two cases in the match statement versus three types of fruit but I would have hoped the compiler would be smart enough to know that using a wildcard is equivalent to matching against each of the remaining cases. If I enumerate them individually as in case Apple | Banana => ??? this works, but it doesn't seem like I should have to do that.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions