引用一個函式

職能是 Kotlin 的一等公民。你可以使用雙冒號獲取它的引用,然後將其傳遞給另一個函式:

fun isPositive(x: Int) = x > 0

val numbers = listOf(-2, -1, 0, 1, 2)
println(numbers.filter(::isPositive)) // [1, 2]