在Kotlin中:: ::运算符有什么用? [英] What is the use of :: operator in Kotlin?
本文介绍了在Kotlin中:: ::运算符有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请给我的示例和用法::科特林中的 操作员
推荐答案
One example: It's for function references, that can be used alternatively to lambdas in many places:
//Function expecting a lambda to be passed
fun <T> applyToList(list: List<T>, func: (T) -> Boolean) = list.filter { it -> func(it) }
fun foo(i: Int): Boolean = i > 3
//call applyToList with reference to foo()
applyToList(list, ::foo)
或与lambda相同:
Or the same with lambda:
applyToList(sub) { it > 3 }
这篇关于在Kotlin中:: ::运算符有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文