Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
arturaz opened this issue Feb 6, 2025 · 1 comment

Comments

@arturaz
Copy link

arturaz commented Feb 6, 2025

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

@arturaz arturaz changed the title Scala 3.6.3: pattern matchin abstract types is broken Scala 3.6.3: pattern matching abstract types is broken in presence of ClassTag Feb 6, 2025
@SethTisue SethTisue transferred this issue from scala/scala-lang Feb 7, 2025
@SethTisue
Copy link
Member

@sjrd wrote on Discord:

ClassTag is unsound for pattern matching. It's supposed to warn was soon as you attempt to use it to do that kind of match.
Use TypeTest/Typeable instead on Scala 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants