I’ve encountered a problem with exhaustivity checking and tried to use the @unchecked annotation and ran into another compiling issue, so wanted to check if there is another way to use the annotation or I’m doing something wrong
package com.scala.example
sealed trait Fruit
class Apple extends Fruit
class Banana extends Fruit
abstract class Stack[T <: Fruit]
class AppleStack extends Stack[Apple]
class BananaStack extends Stack[Banana]
object Main {
def main(args: Array[String]): Unit = {
val appleStack = new AppleStack
whichStack(appleStack)
}
def whichStack(stack: Stack[_]): Unit = {
stack match {
case _: AppleStack => println("This is apple stack")
case _: BananaStack => println("This is banana stack")
}
}
}
Compiling this with latest scala 2.13.5 with sbt-tpolecat flags I encounter
match may not be exhaustive.
[error] It would fail on the following input: (x: com.scala.example.Stack[?] forSome x not in (com.scala.example.AppleStack, com.scala.example.BananaStack))
[error] stack match {
[error] ^
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
Following the suggestion of annotating with @unchecked making the line (stack: @unchecked) match {
runs into another compilation failure
type arguments [_$1] do not conform to class Stack's type parameter bounds [T <: com.scala.example.Fruit]
[error] (stack: @unchecked) match {
[error] ^
[info] Nothing <: _$1?
[info] true
[info] _$1 <: com.scala.example.Fruit?
[info] false
[error] one error found
[error] (Compile / compileIncremental) Compilation failed