Last active
March 12, 2019 12:20
-
-
Save renaudcerrato/f953cf6d01fbfb95a1d370175b234df3 to your computer and use it in GitHub Desktop.
Kotlin Return with Labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val list = listOf("Kotlin", "", "Java", "Groovy") | |
fun main() { | |
list.forEach { | |
if(it.isNullOrEmpty()) return@forEach // return with implicit label | |
println(it) | |
} | |
println("Done!") | |
} | |
// equivalent to: | |
fun main() { | |
list.forEach there@{ str -> | |
if(it.isNullOrEmpty()) return@there // return with explicit label | |
println(str) | |
} | |
println("Done!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment