Skip to content

Scala 3.6.3: pattern matching abstract types is broken in presence of ClassTag #22547

Open
@arturaz

Description

@arturaz
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions