Is this the right way to initialize null references in Scala?

Let's say I have an instance of MyObject that is not initialized:

 var a:MyObject = null 

Is this the correct way to initialize it to zero?

+47
null scala
Mar 13 '10 at 21:38
source share
4 answers

Alternatives

Use null as a last resort. As already mentioned, Option replaces most uses of null. If you use null to implement deferred field initialization with some expensive calculation, you should use lazy val .

Canonical initialization to zero

However, Scala supports null . I personally use it in combination with Spring Injection of Dependency.

Your code works fine. However, I suggest you use var t: T = _ to initialize t its default value. If t is primitive, you get a default value that matches the type. Otherwise, you will get null .

Not only is this more concise, but necessary when you do not know in advance what t will be:

 scala> class A[T] { var t: T = _ } defined class A scala> new A[String].t res0: String = null scala> new A[Object].t res1: java.lang.Object = null scala> new A[Int].t res2: Int = 0 scala> new A[Byte].t res3: Byte = 0 scala> new A[Boolean].t res4: Boolean = false scala> new A[Any].t res5: Any = null 

Additionally

Using var t: T= null is a compilation error if T is unlimited:

 scala> class A[T] { var t: T = null } <console>:5: error: type mismatch; found : Null(null) required: T class A[T] { var t: T = null } 

You can add an implicit parameter as evidence that t is NULL - the AnyRef subtype AnyRef not a NotNull subtype It's not completely baked , even in Scala 2.8, so just think about it now.

 scala> class A[T](implicit ev: Null <:< T) { var t: T = null } defined class A 
+56
Mar 13
source share

The canonical answer does not use null . Use the option type instead:

 var a = None : Option[MyObject] 

If you want to install it:

 a = Some(foo) 

And when you want to read from it, check on None:

 a match { case None => Console.println("not here") case Some(value) => Console.println("got: "+value) } 
+27
Mar 13 '10 at 21:48
source share

As mentioned by David and the retron, it is recommended to use Option in most cases, since Option makes it more obvious that you have to handle the situation without result. However, returning Some(x) requires creating an object, and calling .get or .getOrElse can be more expensive than if-statement. Thus, when using high-performance code, using Option not always the best strategy (especially in the search code, where you can find the value very many times and do not want, accordingly, a lot of creating objects). Again, if you are doing something like returning the text of an entire web page (which may be missing), there is no reason not to use the parameter.

Also, just to add to the retron method for generics with null , you can do this in full if you really mean that it must be null :

 class A[T >: Null] { var t: T = null } 

and it works in 2.7 and 2.8. This is slightly less general than the <:< method, because it does not obey NotNull AFAIK, but otherwise does exactly what you would hope to do.

+8
Mar 14 '10 at 6:11
source share

I came across this question since scalastyle told me not to use null when initializing an object in my test using null .

My solution without changing any type satisfying scalastyle:

 var a: MyObject = (None: Option[MyObject]).orNull 
0
Feb 15 '17 at 13:52
source share



All Articles