Unit vs Nothing in Scala

Viewed 1816

I'd like to clarify some details about Unit and Nothing.

For example:

def unit(companyId: Int): Unit = {}

def Nothing(companyId: Int): scala.Nothing = {}

According to the docs, Unit in Scala is very similar to void in Java. But

test("test Unit and Nothing") {
  println(unit(1))
}
  • Unit return - ()

  • Nothing does not return anything.

Why does Scala has overflow type Unit which return Nothing? Why Scala cannot useNothing instead of Unit?

1 Answers
Related