Reference to method of a particular instance in Kotlin

后端 未结 1 656
傲寒
傲寒 2020-12-02 22:06

In Java 8 we can have a reference to a method of a Class\' instance. Here\'s an example

Function1 ref = a::getItem;

相关标签:
1条回答
  • 2020-12-02 22:49

    Since Kotlin 1.1, you can use bound callable references to do that:

    val f = a::getItem
    

    list.forEach(myObject::myMethod)
    

    Earlier Kotlin versions don't have this feature and require you to make a lambda every time except for these simple cases.

    0 讨论(0)
提交回复
热议问题