So this code wouldn't even do the thing I had plan...
# announcements
k
So this code wouldn't even do the thing I had planned, but still. Why is it asking for a return statement? Any code below this line should be marked as unreachable. The right part of the assignment returns
Nothing
. This doesn't happen if assigned to a new var/val
l
Because an assignment is a statement, not an expression. You don't always throw as far as the compiler can see.
k
What do you mean?
l
I mean that your code is wrong, it's intended behavior that compiler tells you there's an error.
The body of your override doesn't evaluate to the
Nothing
type.
l
Copy code
override fun next(): T {
	val currNext = curr.next
	if (currNext == null) {
		throw NoSuchElementException()	// <- "returns" a value. This is OK
	} else {
		curr = currNext					// <- this is just a statement
	}
}
because the
return
in
return it.value
is not actually to
next()
, but the closure of
let {}
k
yes it is
If you store the returned value in a new val without an explicit type it notices