Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
//about 10 more types
Transaction.Type.BLOCKED -> {
if (transaction.type == Transaction.Type.BLOCKED && transaction.closeAnyway) {
close(transaction)
break //close if type is blocked and has 'closeAnyway' flag
//common logic
//other types
I cannot write
break
:
'break' and 'continue' are not allowed in 'when' statements. Consider using labels to continue/break from the outer loop.
Is it a way to
return/break
from
when
statements? Or what is the best way to solve it?
–
//about 10 more types
Transaction.Type.BLOCKED -> run {
if (transaction.type == Transaction.Type.BLOCKED && transaction.closeAnyway) {
close(transaction)
return@run //close if type is blocked and has 'closeAnyway' flag
//common logic
//other types
–
You can use labels to break/continue/return. e.g.:
transactions@ for (transaction in transactions) {
when (transaction.state) {
Transaction.Type.EXPIRED,
Transaction.Type.BLOCKED -> {
break@transactions
See Returns and Jumps - Kotlin Programming Language for more details.
–
–
//about 10 more types
Transaction.Type.BLOCKED -> {
if (type == Transaction.Type.BLOCKED && closeAnyway) {
close(this)
return@apply
//common logic
//other types
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.