Kotlin has Nothing but there is nothing like Nothing in Java

A pun-filled post on kotlin.Nothing type

Bob
Bob
Aug 25, 2017 · 4 min read
Nothing.

Before going to Nothing, first, let’s look at its sibling Unit.

Unit:

Unit in Kotlin corresponds to the void in Java. Like void, Unit is the return type of any function that does not return any meaningful value, and it is optional to mention the Unit as the return type. But unlike void, Unit is a real class (Singleton) with only one instance.

Nothing:

Nothing is a type in Kotlin that represents “a value that never exists”, that means just “no value at all”.

Nothing can be used as the return type of a function that never returns the code execution — like, looped forever or always throws Exception. Don’t get confused because Java does not have anything similar to the Nothing type.

So Kotlin has this fancy thing, what’s the big deal, you might ask.

Let me show you three examples of how the Nothing as the return type is useful.

For the below examples let’s assume there is a function reportError() which always throws a RuntimeException. In a real application, this reportError() might add some extra useful logs before throwing the exception or throws different exceptions based on different conditions.

reportError() in Java:

reportError() in Kotlin:

Note that the Nothing has to be mentioned explicitly as the return type, otherwise the return type would still be Unit.

1. Unreachable code

What will happen if we mistakenly put some code after calling this reportError() method?

In Java, nothing will happen.

In Kotlin, something good happens because of the Nothing.

There is a warning “Unreachable Code” in Kotlin, whereas Java compiler has no idea about that. And Kotlin saves us from a potential bug using Nothing.

2. Throw in a conditional ternary operator:

What will happen if we call reportError() in a conditional ternary operator?

We will get a compile error “Incompatible types” in Java, as the reportError returns void and we are trying to save it in a String.

In Kotlin (using the Elvis operator as there is no conditional ternary operator, yet):

It results in a compile error in Java, as the compiler couldn’t figure out that the reportError() will never return. But it’s not a problem for Kotlin compiler as it has Nothing.

3. Throw in a method that has a meaningful return type:

What will happen if we call the reportError() in a method that has a return type other than void or Unit?

In Java, we will get a “Missing return statement” error.

In Kotlin, no issues:

Personally, I have been annoyed quite a few times with the above issue while working in Java. I had to put fake return statements — like, return null or return -1 — to workaround the “Missing return statement” error.

So remember to use Nothing whenever the functions don’t return anything.

Please leave your thoughts in the comments or reach me on Twitter.

Thoughts Overflow

My thoughts on Coding, Android, UI/UX, Life, Love

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store