mardi 12 février 2019

How can I implement KFunctionN in Kotlin?

If I define

class MyF : KFunction0<Int> {
    override val name: String = "f"
    override fun invoke() = 42

    override val annotations: List<Annotation>
        get() = TODO()
    ....
}

I can write

val f0: KFunction0<Int> = MyF()
assertEquals("f", f0.name)

but if I try

assertEquals(42, f0())

I get java.lang.ClassCastException: MyF cannot be cast to kotlin.jvm.functions.Function0

How can I define my own implmentation of KFunction0?

I can't work with () -> Int because I need the name property.





Aucun commentaire:

Enregistrer un commentaire