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

Implementation and spec about Null's supertypes are out of sync #11479

Open
Jasper-M opened this Issue Apr 10, 2019 · 3 comments

Comments

Projects
None yet
3 participants
@Jasper-M
Copy link
Member

commented Apr 10, 2019

The only thing the spec has to say about Null, in paragraph 3.5.2 Conformance, is:

For every class type T such that T <: scala.AnyRef one has scala.Null <: T.

But the implementation says:

scala> trait Foo extends Any
defined trait Foo

scala> val foo: Foo = null
foo: Foo = null

scala> implicitly[Null <:< Foo]
res4: Null <:< Foo = generalized constraint
@sjrd

This comment has been minimized.

Copy link
Member

commented Apr 10, 2019

The spec is wrong and the implementation is correct.

There is no choice, because if you declare a class Bar extends AnyRef with Foo, then you can construct a transitive chain of conformance from Null up to Foo:

$ scala
Welcome to Scala 2.12.8 (OpenJDK 64-Bit Server VM, Java 1.8.0_191).
Type in expressions for evaluation. Or try :help.

scala> trait Foo extends Any
defined trait Foo

scala> class Bar extends AnyRef with Foo
defined class Bar

scala> implicitly[Null <:< Bar]
res0: Null <:< Bar = <function1>

scala> implicitly[Bar <:< Foo]
res1: Bar <:< Foo = <function1>

scala> implicitly[Null <:< Foo]
res2: Null <:< Foo = <function1>

The only correct answer is Yes, Null <: Foo, because we have Null <: Bar and Bar <: Foo.

@SethTisue

This comment has been minimized.

Copy link
Member

commented Apr 10, 2019

"The change is reflected in my local copy of the spec" — scala/scala@0149424

looks like the spec change never made it to the repo

@SethTisue SethTisue added this to the Backlog milestone Apr 10, 2019

@Jasper-M

This comment has been minimized.

Copy link
Member Author

commented Apr 10, 2019

Also might be worth updating the Null scaladoc:

Null is - together with scala.Nothing - at the bottom of the Scala type hierarchy.

Null is a subtype of all reference types; its only instance is the null reference. Since Null is not a subtype of value types, null is not a member of any such type. For instance, it is not possible to assign null to a variable of type scala.Int.

It's not strictly incorrect, but it gives the impression that only reference types are supertypes of Null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.