Do Fun Interfaces work with extension functions? `...
# announcements
b
Do Fun Interfaces work with extension functions?
Copy code
fun interface PropHandler<P> {
    fun P.handle(): Unit
}
👌 1
k
Yes.
Copy code
fun interface PropHandler<P> {
    fun P.handle()
}

fun <P> a(handler: PropHandler<P>) {}

fun main() {
    a<String> { }
}
b
and how do I invoke
handle
on a String?
Copy code
fun <P> a(handler: PropHandler<P>) {
    // invoke handler on object of type P
}
k
Ah, now I understand your problem. You will need an auxiliary function like so:
Copy code
fun a(handler: PropHandler<String>) {
    val string = "Hello World"
    
    handler.invoke(string)
}

private fun <P> PropHandler<P>.invoke(value: P){
    value.handle()
}
a
with(handler) { obj_of_type_p.handle() }
👍 1
b
Arkadii’s solution did it. Thank you both very much 🙂
k
Right, I forgot to mention
with
. Thanks Arkadii for pointing that one out. If you want to know a bit more about the why, you can take a look at this thread https://kotlinlang.slack.com/archives/C0922A726/p1604502966429600?thread_ts=1604498090.426500&amp;cid=C0922A726.
👍 2