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

asInstanceOf always infers Nothing #1754

Closed
scabug opened this issue Feb 28, 2009 · 5 comments
Closed

asInstanceOf always infers Nothing #1754

scabug opened this issue Feb 28, 2009 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Feb 28, 2009

scala> val x: AnyRef = "abc".asInstanceOf
java.lang.ClassCastException: java.lang.String cannot be cast to scala.runtime.Nothing$$

scala> val x: Any = 5.asInstanceOf       
java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.runtime.Nothing$$
@scabug
Copy link
Author

scabug commented Feb 28, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1754?orig=1
Reporter: @paulp

@scabug
Copy link
Author

scabug commented Mar 17, 2009

@odersky said:
This would be possible only if we special case asInstanceOf, and I don't think we should do that.

@scabug
Copy link
Author

scabug commented Mar 17, 2009

@paulp said:
That's fine. I ask because I couldn't figure out what ticket #109 was trying to report, and it used asInstanceOf that way:

    def complete(sym : global.Symbol) = {
      Console.println("completing " + sym)
      typeParser.parseType(sym.asInstanceOf)
    }

@scabug
Copy link
Author

scabug commented Aug 4, 2011

@paulp said:
OK, it took me 2.5 years but I have to reopen this (reminded of it by #4871.) Why would this imply special casing asInstanceOf? As far as I can see it's the opposite: asInstanceOf is being given special status by not having to act the way everything else does.

Easily summarized:

scala> class Bip { def empty[T: Manifest, CC[_]] = { println(manifest[T]) ; null.asInstanceOf[CC[T]] } }
defined class Bip

scala> val x: Set[Int] = (new Bip).empty
Int
x: Set[Int] = null

scala> val x: Set[Int] = (new Bip).asInstanceOf
java.lang.ClassCastException: Bip cannot be cast to scala.runtime.Nothing$

Upon what basis should it infer Set[Int] when given nothing to go upon but the expected type, but in the same situation asInstanceOf should infer Nothing?

@scabug scabug closed this as completed May 5, 2012
@scabug
Copy link
Author

scabug commented May 5, 2012

@paulp said:
I'm still not quite sure, but I lack sufficient curiosity to prolong.

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

2 participants