Non-Local Returns (#9934)

This commit is contained in:
Mona Mohamadinia 2020-09-08 21:09:16 +04:30 committed by GitHub
parent 0f42bcb243
commit 0748a7534f
1 changed files with 24 additions and 0 deletions

View File

@ -22,6 +22,30 @@ fun main() {
numbers.each { println(random * it) } // capturing the random variable
}
fun namedFunction(): Int {
return 42
}
fun anonymous(): () -> Int {
return fun(): Int {
return 42
}
}
inline fun <T> List<T>.eachIndexed(f: (Int, T) -> Unit) {
for (i in indices) {
f(i, this[i])
}
}
fun <T> List<T>.indexOf(x: T): Int {
eachIndexed { index, value ->
if (value == x) return index
}
return -1
}
/**
* Generates a random number.
*/