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
Compiler and runtime inconsistent as to whether Null <: AnyVal #1470
Comments
Imported From: https://issues.scala-lang.org/browse/SI-1470?orig=1 |
@lrytz said: var x: AnyVal = _ |
@SethTisue said (edited on Sep 3, 2016 12:18:51 AM UTC): one of the "other situations" Lukas mentions is with generic types: scala> class Sloppy[A] { var value: A = _ }
defined class Sloppy
scala> new Sloppy[Double].value
res14: Double = 0.0
scala> new Sloppy[Double].value: Any
res15: Any = null
scala> new Sloppy[Double].value: AnyVal
res16: AnyVal = null
scala> println(new Sloppy[Double].value)
null |
@SethTisue said: |
@soc said: |
Oliver Ruebenacker (curoli) said: object GenInitApp extends App { class Unspecialized[T] { var x: T = _ } println("Unspecialized x is " + (new Unspecialized[Float]).x) } Output is: Unspecialized x is null |
@som-snytt said (edited on Sep 10, 2016 6:33:35 AM UTC): var x: String = if (x == null) null else x // don't munge early setting I was just looking at a field that was side-effected from early definition, so the explicit definition would have to be early, too, and not just earlier in the template body. The comment on the warning: bq. |
Adding two cents here: deprecating the public class Producer<A> {
A get() {
return null;
}
} And call it from Scala: val p = new Producer[Boolean]
val bool = p.get()
println(bool) // prints "false"
println(bool.getClass) // prints "boolean"
println(p.get()) // prints "null"
println(p.get().getClass) // prints "boolean" |
@gaelrenoux Starting Scala 3 Dotty, it will actually be a non issue. A variable cannot be assigned to null unless the type is a union type with Null. So in your example, returning null as A is illegal in Scala. As for Java interface, all values from Java library output is implicitly a union type with Null. |
Expected type of
|
Neither or both of the following should work, but having one working and not the other is inconsistent:
The text was updated successfully, but these errors were encountered: