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

Problems with polymorphic method, Nothing, Invariant return type #1570

Open
scabug opened this issue Dec 8, 2008 · 8 comments
Open

Problems with polymorphic method, Nothing, Invariant return type #1570

scabug opened this issue Dec 8, 2008 · 8 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) typelevel typer
Milestone

Comments

@scabug
Copy link

scabug commented Dec 8, 2008

Happening in Scala 2.7.1 and 2.7.2 at least:

scala> trait Test[A]
defined trait Test

scala> trait Test2[+A]
defined trait Test2

scala> def f[A](a:Test[A]):Test[A] = null
f: [A](Test[A])Test[A]

scala> def f2[A](a:Test[A]):Test2[A] = null
f2: [A](Test[A])Test2[A]

scala> val x:Test[Nothing] = null
x: Test[Nothing] = null

scala> f(x)
<console>:8: error: type mismatch;
 found   : Test[Nothing]
 required: Test[A]
      f(x)
        ^

scala> f[Nothing](x)
res2: Test[Nothing] = null

scala> f2(x)
res1: Test2[Nothing] = null

This happens only for A=Nothing. And only if the return type is invariant in a type variable being Nothing. There is probably not much sense in operating on Nothing types, but what is the rationale behind this behaviour?

@scabug
Copy link
Author

scabug commented Dec 8, 2008

Imported From: https://issues.scala-lang.org/browse/SI-1570?orig=1
Reporter: Johannes Rudolph (jrudolph100)

@scabug
Copy link
Author

scabug commented Dec 12, 2008

Mohsen Lesani [X] (lesani) said:

This is still a problem in Trunk.

@scabug
Copy link
Author

scabug commented Feb 13, 2011

@soc said:
Slightly different error for f(x) in 2.9.0.r24145-b20110130020134

scala> f(x)
<console>:9: error: type mismatch;
 found   : Test[Nothing]
 required: Test[A]
Note: Nothing <: A, but trait Test is invariant in type A.
You may wish to define A as +A instead. (SLS 4.5)
       f(x)
         ^

@scabug
Copy link
Author

scabug commented May 3, 2011

@adriaanm said:
see also #4509

@scabug
Copy link
Author

scabug commented Jul 23, 2014

Daniel Darabos (darabos) said:
What an old and annoying issue. I wouldn't know where to start fixing it. But if someone can give a rough outline of how this could be fixed, I can offer to work on the implementation. Thanks.

@dima-starosud
Copy link

Guys, should we try to adopt that as a quick-and-dirty fix for this?

@SethTisue

This comment has been minimized.

@smarter
Copy link
Member

smarter commented Sep 30, 2020

@SethTisue You must have done something weird in your repl session, I just tried it and all these examples typecheck in Dotty 0.27.0-RC1: https://scastie.scala-lang.org/smarter/aTB7lTdPTVa7wSI5dEW4wA/5

@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) typelevel typer
Projects
None yet
Development

No branches or pull requests

6 participants