lifter
08/28/2018, 8:12 PMNothing
in this case...
fun justThrowIt(): Nothing = throw IOException()
...while here, I'm not:
fun justLoop() { while (true) {} }
Dominaezzz
08/28/2018, 8:14 PMlifter
08/28/2018, 8:15 PMNothing
and it does still compile.Andreas Sinz
08/28/2018, 8:15 PMfun justThrowIt() { throw IOException() }
works toolifter
08/28/2018, 8:16 PM"'Nothing' return type needs to be specified explicitly."
Dominaezzz
08/28/2018, 8:16 PMAndreas Sinz
08/28/2018, 8:17 PMDominaezzz
08/28/2018, 8:17 PMthrow ...
is not an expression.lifter
08/28/2018, 8:18 PMthrow
is indeed an expression.Dominaezzz
08/28/2018, 8:18 PMlifter
08/28/2018, 8:18 PMval result = try {
println("Doing some I/O...")
true
} catch (e: IOException) {
println("Oops!")
false
} finally {
println("""In the "finally" block...""")
}
Dominaezzz
08/28/2018, 8:19 PMlifter
08/28/2018, 8:19 PMDominaezzz
08/28/2018, 8:19 PMval something = throw IOException()
?lifter
08/28/2018, 8:19 PMval x = throw IOException()
Totally compiles.Dominaezzz
08/28/2018, 8:20 PMlifter
08/28/2018, 8:20 PMx
appears to be Nothing
, at least it still compiles if I explicitly state it as such.Andreas Sinz
08/28/2018, 8:20 PMthrow
always "returns" Nothing
lifter
08/28/2018, 8:21 PMAndreas Sinz
08/28/2018, 8:21 PMwhile
is a statement, so a while-loop doesn't return anythingfun justLoop() = while(...) { }
doesn't compilelifter
08/28/2018, 8:22 PMDominaezzz
08/28/2018, 8:23 PMthrow
"returns" Nothing
, then why doesn't fun justThrowIt() = throw IOException()
work? Strange.lifter
08/28/2018, 8:23 PMfun 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.Nothing
as the return type, as in
fun justThrowIt(): Nothing = throw IOException()
That will compile.Andreas Sinz
08/28/2018, 9:06 PMNothing
from a functionDominaezzz
08/28/2018, 9:08 PM