Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usages of Null / Nothing / Unit in Scala

Tags:

scala

I've just read: http://oldfashionedsoftware.com/2008/08/20/a-post-about-nothing/

As far as I understand, Null is a trait and its only instance is null.

When a method takes a Null argument, then we can only pass it a Null reference or null directly, but not any other reference, even if it is null (nullString: String = null for example).

I just wonder in which cases using this Null trait could be useful. There is also the Nothing trait for which I don't really see any more examples.


I don't really understand either what is the difference between using Nothing and Unit as a return type, since both doesn't return any result, how to know which one to use when I have a method that performs logging for example?


Do you have usages of Unit / Null / Nothing as something else than a return type?

like image 560
Sebastien Lorber Avatar asked Apr 23 '13 15:04

Sebastien Lorber


People also ask

What is difference between null and nothing?

The semantics of Null are very poorly understood, particularly amongst people who have little experience with programming. Empty says “I'm an uninitialized variant,” Nothing says “I'm an invalid object” and Null says “I represent a value which is not known.” Null is not True, not False, but Null !

How do you use null in Scala?

In Scala, using null to represent nullable or missing values is an anti-pattern: use the type Option instead. The type Option ensures that you deal with both the presence and the absence of an element. Thanks to the Option type, you can make your system safer by avoiding nasty NullPointerException s at runtime.

What's the difference nil null none and nothing in Scala?

Null is a subtype of all the reference classes. null is its only instance. Nothing is subtype of every other type i.e of reference and value classes.

Is using null in Scala a good practice?

As a word of caution (and balance), the Twitter Effective Scala page recommends not overusing Option , and using the Null Object Pattern where it makes sense. As usual, use your own judgment, but try to eliminate all null values using one of these approaches.


1 Answers

You only use Nothing if the method never returns (meaning it cannot complete normally by returning, it could throw an exception). Nothing is never instantiated and is there for the benefit of the type system (to quote James Iry: "The reason Scala has a bottom type is tied to its ability to express variance in type parameters."). From the article you linked to:

One other use of Nothing is as a return type for methods that never return. It makes sense if you think about it. If a method’s return type is Nothing, and there exists absolutely no instance of Nothing, then such a method must never return.

Your logging method would return Unit. There is a value Unit so it can actually be returned. From the API docs:

Unit is a subtype of scala.AnyVal. There is only one value of type Unit, (), and it is not represented by any object in the underlying runtime system. A method with return type Unit is analogous to a Java method which is declared void.

like image 55
Nathan Hughes Avatar answered Oct 14 '22 16:10

Nathan Hughes