Use || instead | for pattern matching 'or' patterns to free up | for matching union types #16587
-
Scala 3 has union types but unfortunately they can't be used in pattern matching since By using final case class Cat(name: String)
final case class Dog(name: String)
val animal = Cat(name = "Puffy")
// or pattern matching
animal match {
case Cat(name) || Dog(name) => println(s"cat or dog: $name")
}
// Union pattern matching
animal match {
case a: Cat | Dog => println(s"cat or dog: ${a.name}")
}
This was inspired by dart 3. See https://github.com/dart-lang/language/blob/master/accepted/future-releases/0546-patterns/feature-specification.md#logical-or-pattern Naturally this might require considerable migration. But still worth taking a look at since union types matching could be very useful. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You can do case a: Cat | Dog => |
Beta Was this translation helpful? Give feedback.
Sorry, got my syntax wrong. I meant