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 implementation of KFunction0
?
I can't work with () -> Int
because I need the name
property.
I'm using Kotlin 1.3.21.
Additionally - it seems that I can run
val f02 = MyF()
assertEquals(42, f02())
My actual use case is Can I convert a Kotlin KFunction1 to a KFunction0 by applying the argument?
It is definitely not a good idea to explicitly implement an internal class from Kotlin JVM. You do not have code completion for that classes in IntelliJ or Android Studio for a reason
You may use callable references instead, to make Kotlin compiler generate all necessary classes for you. https://kotlinlang.org/docs/reference/reflection.html#callable-references
The benefit - future version of Kotlin will unlikely to break that code (and may break you inheritor class)
Should you need a longer name for the function, you may declare it like that
In Kotlin 1.2 I haven't found
KFunction0<T>
but onlyKFunction<T>
and I was able to do what you wanted:I will update Kotlin to 1.3 later and I'll complete my answer (if it works in Kotlin 1.3).
So where can be your problem? Maybe (just lucky guess) check type based on overriden methods like
returnType
?Edit:
After migrating to Kotlin 1.3 I was able to extend
KFunction0<Int>
instead ofKFunction<Int>
. The only changes - my class has to also overrideinvoke(): Int
. Still works!Edit2:
I'm not sure if I just didn't saw KFunction0 in my IDE or is not present in Kotlin below 1.3.