Kotlin interface to lambda? (Any Trick)
# announcements
m
Kotlin interface to lambda? (Any Trick)
c
Do you mean something like?:
Copy code
interface MyInterface {
    fun myFunction()
}

fun usesMyInterface(myInterface: MyInterface) { }

usesMyInterface(object : MyInterface {
    override fun myFunction() {
        
    }
})
s
@Can Orhan not a lambda though
interestingly this compiles
Copy code
fun main() {
    usesMyInterface({name: String -> println("hello $name")} as MyInterface)
}

interface MyInterface {
    fun myFunction(s: String)
}

fun usesMyInterface(myInterface: MyInterface) {
    myInterface.myFunction("Yoda")
}
but throws an error. https://pl.kotl.in/_stgLor9V
c
Yeah sorry 😄 I had difficulty understanding exactly what the question wanted in the first place. I should have just said nothing 😄
s
You want SAM conversions for kotlin interfaces. There is an open ticket for it.
c
Rather than defining it as an interface, maybe try writing it as a typealias.
typealias MyInterface = (s: String)->Unit