Open
Description
import scala.reflect.ClassTag
enum MyEnum {
case A
case B
}
enum MyBoxEnum {
case A(a: MyEnum.A.type)
case B(b: MyEnum.B.type)
}
def whatIsIt[Kind <: MyEnum : ClassTag](value: MyEnum): String = {
value match {
case k: Kind => s"kind: $k"
case other => s"other: $other"
}
}
val value = MyEnum.A
// Correctly returns "kind: A"
whatIsIt[MyEnum.A.type](value)
// Should return "other: A", returns "kind: A"
whatIsIt[MyEnum.B.type](value)
def whatIsItBox[Kind <: MyEnum : ClassTag](value: MyBoxEnum): String = {
// If ClassTag is present compiler does not tell me that I'm matching completely
// unrelated types
value match {
case k: Kind => s"kind: $k"
case other => s"other: $other"
}
}
val boxValue = MyBoxEnum.A(MyEnum.A)
// Should return "kind: A(A)", returns "other: A(A)"
whatIsItBox[MyEnum.A.type](boxValue)
// Correctly returns "other: A(A)"
whatIsItBox[MyEnum.B.type](boxValue)
https://scastie.scala-lang.org/KqbPepV7ReGR8Sz6VplTvQ
Discussion in Scala discord: https://discord.com/channels/632150470000902164/632150470000902166/1337140606493003869