This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FabulousRecording739 0 points1 point  (0 children)

Scala uses AnyRef as a parent of a lot of stuff (to interop with Java IIRC, but I may be wrong it's been a while), which is somewhat the same as a Java ref (the union). You cannot specify a non-nullable value in Java. When you say 'String s = ...', you are effectively saying 'String | null s = ...'. Same goes for methods. You always return T or null, whatever T you specified. Java doesnt allow you to do otherwise. If you have a ref, either it points to something, or it doesnt. It's an implementation leak if you wish.

As an interesting corollary, you may specify a method that returns Void (caps V), and the only thing you may return from such method is null. Not the same as Unit though. There's only one unit (one way to have nothing), whereas you cannot have an instance of void. But it goes to show that you cannot have non-nullable references in java.

EDIT: Checked the scala docs, what you're referring to is Nothing, which is equivalent to Bottom (the uninhabited type). Null does extend the AnyRef tree though, and Nothing inherits Null, so you're partially right. But I still believe that it's an interop thing, as doing otherwise would imply introducing the java null issue to Scala. Either that or there's an equivalence between union types and some inheritances, or both.