https://kotlinlang.org logo
Title
l

lifter

08/28/2018, 8:12 PM
It seems a bit odd that I'm required to explicitly state the return type of
Nothing
in this case...
fun justThrowIt(): Nothing = throw IOException()
...while here, I'm not:
fun justLoop() { while (true) {} }
d

Dominaezzz

08/28/2018, 8:14 PM
The second function "returns" Unit.
☝️ 1
l

lifter

08/28/2018, 8:15 PM
I can explicitly state the return type as
Nothing
and it does still compile.
a

Andreas Sinz

08/28/2018, 8:15 PM
fun justThrowIt() { throw IOException() }
works too
l

lifter

08/28/2018, 8:16 PM
No, it doesn't.
"'Nothing' return type needs to be specified explicitly."
d

Dominaezzz

08/28/2018, 8:16 PM
If you omit the return type, you've explicitly made it Unit.
a

Andreas Sinz

08/28/2018, 8:17 PM
my version is different from your version
d

Dominaezzz

08/28/2018, 8:17 PM
If the method is an expression, then the return type is inferred.
IIRC,
throw ...
is not an expression.
l

lifter

08/28/2018, 8:18 PM
@Andreas Sinz Oh gosh, you're right, it is different.
throw
is indeed an expression.
d

Dominaezzz

08/28/2018, 8:18 PM
Oh is it?
l

lifter

08/28/2018, 8:18 PM
Yeah:
val result = try {
    println("Doing some I/O...")
    true
} catch (e: IOException) {
    println("Oops!")
    false
} finally {
    println("""In the "finally" block...""")
}
d

Dominaezzz

08/28/2018, 8:19 PM
That's try and catch, not throw.
l

lifter

08/28/2018, 8:19 PM
Ahaha sorry.
d

Dominaezzz

08/28/2018, 8:19 PM
Can you do,
val something = throw IOException()
?
:yes: 4
l

lifter

08/28/2018, 8:19 PM
Anyway, yeah:
val x = throw IOException()
Totally compiles.
d

Dominaezzz

08/28/2018, 8:20 PM
Oh, had no idea. Weird.
l

lifter

08/28/2018, 8:20 PM
And the type of
x
appears to be
Nothing
, at least it still compiles if I explicitly state it as such.
a

Andreas Sinz

08/28/2018, 8:20 PM
throw
always "returns"
Nothing
l

lifter

08/28/2018, 8:21 PM
OK, cool.
a

Andreas Sinz

08/28/2018, 8:21 PM
and btw,
while
is a statement, so a while-loop doesn't return anything
fun justLoop() = while(...) { }
doesn't compile
l

lifter

08/28/2018, 8:22 PM
Yeah that's true.
d

Dominaezzz

08/28/2018, 8:23 PM
If
throw
"returns"
Nothing
, then why doesn't
fun justThrowIt() = throw IOException()
work? Strange.
l

lifter

08/28/2018, 8:23 PM
So, as far as I can tell, in the case of
fun justLoop() { while (true) {} }
The return type is
Unit
. It does still compile if I explicitly state return type of
Nothing
. I'd guess that the compiler would enforce return type of
Nothing
if it were smart enough to know that the function never returns, but I guess it isn't.
Well it does "work", in a sense, you're only required to state
Nothing
as the return type, as in
fun justThrowIt(): Nothing = throw IOException()
That will compile.
a

Andreas Sinz

08/28/2018, 9:06 PM
@Dominaezzz it does work fine, you get the error Nothing return type needs to be specified explicitly so you don't accidentally return
Nothing
from a function
d

Dominaezzz

08/28/2018, 9:08 PM
Oh okay. That makes sense.