4

According to the scala specification, scala.Nothing type - buttom for all types. Type "Nothing" exists but instance of Nothing does not exists.

How it works:

def ??? : Nothing = throw new NoImplementedError
def sys.error(message: String): Nothing = throw new RuntimeException()
def sys.exit(status: Int): Nothing = {...}

But in fact, all of mentioned methods return exception. Excepted def sys.exit Could you please clarify more about type Nothing. Any examples, explanations.

Thanks!

Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • Possible duplicate of [Usages of Null / Nothing / Unit in Scala](http://stackoverflow.com/questions/16173477/usages-of-null-nothing-unit-in-scala) – Mohitt Mar 07 '16 at 16:32
  • A method which _returned_ an exception would look like `def foo = new RuntimeException`. If you gave it type signature `: Nothing`, it wouldn't compile, because it returns something. – Alexey Romanov Mar 07 '16 at 18:11

2 Answers2

7
def ??? : Nothing = throw new NoImplementedError

does not return an exception, it throws an exception which is not the same thing. Exceptions are a control-flow mechanism which causes control to immediately jump to the nearest installed handler within the call stack. This means that in

val x = ???

x will never be assigned a value, so x can have any type at all. This type is Nothing in the scala type system which is the subtype of all types.

Non-termination also has type nothing since it also never returns a value e.g.

def loop(): Nothing = loop()

val x: Int = loop()

is therefore allowed since x will never be assigned.

Lee
  • 142,018
  • 20
  • 234
  • 287
1

In Scala absolutely everything needs to have a return type.

println for example have the return type Unit because it's a unit of calculations that returns no value.

If everything needs to have a return type then, how would you implement a function like ???, so that it can be used by any function, no matter what type it needs to return?

Well that is what Nothing is for, it's a subtype of every other types (as Any is the top type of every other type), so a function that returns Nothing can be used anywhere and the type system will always stay consistent.

As you already noticed functions returning Nothing never return a value. That is because there is no value instenciable for Nothing.
You can see it as a way to make the compiler "happy" for functions that only throws or stop the app or loop forever.


In case you wonder why we don't use Unit instead of Nothing, here is an example:

def notYetImplemented: Unit = throw new Exception() 
def myFunc: String = notYetImplemented

This will not compile because Unit is not a String or a subtype of String. So you will need to do:

def myFunc: String = {
  notYetImplemented
  null
}

Which is not very convenient and for us to write a value to return even taught we will never reach this code.

Cheers

Joan
  • 4,079
  • 2
  • 28
  • 37
  • Yes, but in fact throw new RuntimeException() return exception instead of Nothing, correct ? –  Mar 07 '16 at 17:25
  • Throwing != returning. – Ryan Mar 07 '16 at 18:20
  • @scala the type `Nothing` is a guaranty that it will never return something. Therfore the other options are throwing an exception or terminating the app or looping for ever. – Joan Mar 07 '16 at 18:24
  • In Scala absolutely everything needs to have a return type. Even if it doesn't return something. – Joan Mar 07 '16 at 18:32