https://kotlinlang.org logo
#announcements
Title
# announcements
k

Kroppeb

07/28/2019, 10:18 PM
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

louiscad

07/28/2019, 10:51 PM
Because an assignment is a statement, not an expression. You don't always throw as far as the compiler can see.
k

Kroppeb

07/29/2019, 9:20 AM
What do you mean?
l

louiscad

07/29/2019, 9:30 AM
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

louis993546

07/29/2019, 6:05 PM
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

Kroppeb

08/01/2019, 4:38 PM
yes it is
If you store the returned value in a new val without an explicit type it notices
3 Views