Reputation: 2697
As stated here - Chapter 7 of Programming in Scala, Built-in Control Structures, 7.4 Exception handling with try expressions:
In Scala, throw is an expression that has a result type.
Technically, an exception throw has type
Nothing
. You can use a throw as an expression even though it will never actually evaluate to anything. This little bit of technical gymnastics might sound weird, but is frequently useful in cases like the previous example. One branch of an if computes a value, while the other throws an exception and computesNothing
. The type of the whole if expression is then the type of that branch which does compute something.
The example is:
val half =
if (n % 2 == 0)
n / 2
else
throw new RuntimeException("n must be even")
Then I went to Scala and try:
scala> val n = 1
n: Int = 1
scala> val half = if (n % 2 == 0) n / 2 else throw new RuntimeException("n must be even")
java.lang.RuntimeException: n must be even
... 29 elided
scala> half
<console>:12: error: not found: value half
half
^
It is saying that half
is not found. However, based on the book, I assume it should say it is defined and it is with type Nothing
.
What's wrong here?
Upvotes: 3
Views: 852
Reputation: 149538
It is saying that half is not found. However, based on the book, I assume it should say it is defined and it is with type Nothing.
If you re-read that paragraph, you'll see that the type of half
shouldn't be Nothing
, it should be Int
:
The type of the whole if expression is then the type of that branch which does compute something.
The branch which does compute a value produces the type Int
. You can prove this by defining half
as a method and not a value:
scala> def half = if (n % 2 == 0) n / 2 else throw new RuntimeException("n must be even")
half: Int
If you actually want to see that throw
has type Nothing
, add it in your IDE and make it show the type:
val exception: Nothing = throw new RuntimeException("n must be even")
Regarding half
, it isn't found because it's declaration throws an exception, which makes the REPL unable to bind a value to it.
Upvotes: 6