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

Return type inference doesn't affect Manifest #2609

Closed
scabug opened this issue Nov 10, 2009 · 5 comments
Closed

Return type inference doesn't affect Manifest #2609

scabug opened this issue Nov 10, 2009 · 5 comments
Assignees

Comments

@scabug
Copy link

scabug commented Nov 10, 2009

What I see:

def inject[T](implicit m: Manifest[T]): T = {
  println("Manifest is: " + m)
  null.asInstanceOf[T]
}

scala> val s = inject[String]
Manifest is: java.lang.String
s: String = null

scala> val s: String = inject
Manifest is: Nothing
s: String = null

I would expect the second manifest to be java.lang.String, since the type T is correctly inferred to be T.

@scabug
Copy link
Author

scabug commented Nov 10, 2009

@scabug
Copy link
Author

scabug commented Nov 10, 2009

@jorgeortiz85 said:
Sorry, meant to say "since the type T is correctly inferred to be String".

@scabug
Copy link
Author

scabug commented Nov 10, 2009

@harrah said:
T is inferred to be Nothing, not String. Nothing is a subtype of String, so the second statement is allowed. More directly,

scala> val s: String = null.asInstanceOf[Nothing]
s: String = null

Of course, Nothing is not supposed to have any instances, so having an actual instance assigned to a val of type Nothing is presumably the cause of the following:

scala> val s = null.asInstanceOf[Nothing]
java.lang.NullPointerException                   
        at RequestResult$$.<init>(<console>:9)    
        at RequestResult$$.<clinit>(<console>)    
        at RequestResult$$result(<console>)
        ...

@scabug
Copy link
Author

scabug commented Nov 20, 2009

@odersky said:
I think I agree with Mark's assessment. The problem is that the expected return type String only gives an upper bound. So it does not constrain the parameter type of the Manifest. So this is in fact as it's supposed to be (given the current type inference algorithm).

@scabug scabug closed this as completed May 18, 2011
@scabug
Copy link
Author

scabug commented Jun 6, 2011

@adriaanm said (edited on Jul 8, 2011 9:31:43 AM UTC):
Here's what -Xprint:typer tells us for variations on this theme. Pretty consistently Nothing.

object Test extends App {
  def inject0[T]: T = null.asInstanceOf[T]
  def m0: String = inject0 // inject0[Nothing] is inferred

  def inject1[T](implicit m: Manifest[T]): T = {println(m.toString); null.asInstanceOf[T]}
  def m1: String = inject1 // inject1[Nothing] is inferred

  class M[T]; implicit def M[T] = new M[T]
  def inject2[T](implicit m: M[T]): T = {println(m.toString); null.asInstanceOf[T]}
  def m2: String = inject2 // inject2[Nothing] is inferred

  m0
  m1
  m2
}

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

No branches or pull requests

2 participants