Navigation Menu

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

Pattern match fails on custom generic type #9648

Closed
scabug opened this issue Feb 5, 2016 · 3 comments
Closed

Pattern match fails on custom generic type #9648

scabug opened this issue Feb 5, 2016 · 3 comments

Comments

@scabug
Copy link

scabug commented Feb 5, 2016

Following up this question on Stack Overflow:

Pattern matching a type using typeOf[T] with @unchecked gives incorrect result.

The code below

import scala.reflect.runtime.{universe => ru} 

class Foo[T](val data: T)

def matchFoo[A: ru.TypeTag](foo: Foo[A]) = {
  foo match {
    case fooStr: Foo[String @unchecked] if ru.typeOf[A] =:= ru.typeOf[String] => println("Found String")
    case _ => println("Found other")
  }
}

def main(args: Array[String]): Unit = {
  matchFoo(new Foo[Int](123))
}

expected result:

Found other

actual result:

Found String

@scabug
Copy link
Author

scabug commented Feb 5, 2016

Imported From: https://issues.scala-lang.org/browse/SI-9648?orig=1
Reporter: Maciej Szopinski (maciej.sz)
Affected Versions: 2.11.5

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@adriaanm adriaanm removed their assignment Sep 28, 2018
@SethTisue
Copy link
Member

SethTisue commented Dec 23, 2023

It's not clear to me that this is a bug. Matching against Foo[String @unchecked] tells the compiler "I don't need you to check that A is String — you may assume it is." And then the compiler proceeds on that assumption.

@som-snytt
Copy link

Apparently because invariant. class Foo[+A] is the expected behavior. (or case _: Foo[_])

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Dec 23, 2023
@SethTisue SethTisue removed this from the Backlog milestone Dec 24, 2023
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

4 participants