IntelliJ suggested return@filter and I am not sure what that is, an annotation?
Thanks
IntelliJ suggested return@filter and I am not sure what that is, an annotation?
Thanks
“In Kotlin, the return@label
syntax is used for specifying which function among several nested ones this statement returns from.”
So, IntelliJ was hinting the return was the filter lambda “^filter”, then adding that made it explicit. The above taken from android - Kotlin: Whats does "return@" mean? - Stack Overflow
This is not a matter of being explicit. It is necessary.
In Kotlin return
statement does not return from lambdas, but from the nearest enclosing function. If you need to return from a lambda then using a label is the only way.
Thanks @broot
This is true for lambdas passed to inline functions (except when crossinline
is used).
In cases where the lambda is used as a callback to be called later, such as onClick {…}
, return
without a label returns from the lambda itself (but the label can still be used for clarity.)
For details:
Either I misunderstand you or links provided by you clearly say that you are incorrect. Even if lambda isn’t inlined, return
still means returning from an enclosing function and for this reason it is actually disallowed. Your second link explains this very clearly:
In Kotlin, you can only use a normal, unqualified
return
to exit a named function or an anonymous function. To exit a lambda, use a label. A barereturn
is forbidden inside a lambda because a lambda cannot make the enclosing functionreturn
It would be pretty error-prone if we would need to check whether the lambda is inlined or not to determine, what return
inside it does.
Yes, you’re right. My mistake.