F. *_*ely 4 first-class-functions kotlin
这不编译的事实是否意味着他们不是一流的类型?
fun foo(s: String): Int = s.length
// This won't compile.
val bar = foo
Run Code Online (Sandbox Code Playgroud)
有没有办法在不诉诸OO的情况下做到这一点?
您可以使用该函数的引用 ::
:
fun foo(s: String): Int = s.length
val bar = ::foo
Run Code Online (Sandbox Code Playgroud)
然后调用它:
bar("some string")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
72 次 |
最近记录: |