Skip to content

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

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

Closed
LPTK opened this issue Jan 31, 2019 · 1 comment
Closed

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

LPTK opened this issue Jan 31, 2019 · 1 comment

Comments

@LPTK
Copy link
Contributor

LPTK commented Jan 31, 2019

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".

@LPTK
Copy link
Contributor Author

LPTK commented Jan 31, 2019

Note that even the following passes the type checker without a warning (at least in the REPL):

scala> def test: List[Int]|List[String] => Int = { case ls: List[Int] => ls.head  case _ => 0 }
def test List[Int] | List[String] => 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)

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

No branches or pull requests

3 participants