Skip to content

Pattern matching is unsound in the presence of type unions #5826

Closed
@LPTK

Description

@LPTK
scala> def test[A]: List[Int] | A => Int = { case ls: List[Int] => ls.head  case _ => 0 }
def test[A] => List[Int] | A => Int

scala> test(List("oops"))
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
	at scala.runtime.BoxesRunTime.unboxToInt(BoxesRunTime.java:103)

The definition of test should at least yield a warning like "erased type argument Int in type pattern List[Int] is unchecked".

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions