Lambda implementation of interface in kotlin

Jocky Doe :

What would be the equivalent of that code in kotlin, nothing seems to work of what I try:

public interface AnInterface {
    void doSmth(MyClass inst, int num);
}

init:

AnInterface impl = (inst, num) -> {
    //...
}
s1m0nw1 :

If AnInterface is Java, you can work with SAM conversion:

val impl = AnInterface { inst, num -> 
     //...
}

Otherwise, if the interface is Kotlin...

interface AnInterface {
     fun doSmth(inst: MyClass, num: Int)
}

...you can use the object syntax for implementing it anonymously:

val impl = object : AnInterface {
    override fun doSmth(inst:, num: Int) {
        //...
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=428189&siteId=1