user5607337
user5607337

Reputation:

scala.Null clarification

Want to clarify about scala.Null. As I know scala.Null has instance null and class. For example if write like this

val x: Null = null 

Type inference set type for the x Null.

According Scala classes hierarhy "please see image" Scala cannot use variables without wrappers like Int, Double, etc. That is why we can found Null for the type inference mechanism resolving. Is it correct vision ?

Scala classes hierarhy

Upvotes: 1

Views: 394

Answers (2)

Jesper
Jesper

Reputation: 206816

The type Null in Scala is a special type. What's special about it is that it is a subtype of all reference types - it's a subtype of every type that is a subtype of AnyRef.

The reason for that is to make it possible to assign the value null to any reference type.

Note that besides reference types, Scala also has value types. These are the types that are subtypes of AnyVal. Examples of value types are the built-in types that map to the primitive types of the JVM: Byte, Short, Int, Long, Float, Double, Boolean, and Char, and also the type Unit.

Note that AnyVal and its subtypes are not subtypes of AnyRef. You can't assign null to a variable of a value type.

Note that the value types Int, Double etc. are not like Java's wrapper types (java.lang.Integer, java.lang.Double etc.). When you use the value types, there is no boxing and unboxing going on to/from objects. These value types directly map to the primitive types of the JVM. Therefore you cannot assign null to a variable of one of those types.

In other words: types like Int and Double are not wrappers, they directly represent primitive types.

Upvotes: 1

Tzach Zohar
Tzach Zohar

Reputation: 37832

You seem to be mixing some of these concepts:

Type Inference is the ability of the compiler to infer the type when the code doesn't explicitly state it. In your example - you do state the type, and that type is Null - no inference here:

val x: Null = null 

Here's a simple example of type inference:

scala> val whoKnows = 12
whoKnows: Int = 12

Compiler inferred the type Int since we assigned the value 12, which is an integer. There's much more to it (see http://docs.scala-lang.org/tutorials/tour/local-type-inference), but that's the basics.

null can be used in Scala just like in Java (although it's very much frowned upon, as Scala offers many safer alternatives, mainly None), for example:

scala> val nullStr: String = null
nullStr: String = null

scala> val nullList: List[Int] = null
nullList: List[Int] = null

Where can't null be used? Just like in Java - for "primitives" like int, double etc. In Scala, there's no such thing as a primitive, but there's still a difference between these "value" types (named with upper-case like other types, e.g. Int, Double) and other types: value types share the common trait AnyVal, whereas all other types share the trait AnyRef - to signify, among other things, this difference.

As for what the Null trait is good for - see Usages of Null / Nothing / Unit in Scala

Upvotes: 2

Related Questions