Mark
03/30/2021, 10:23 AMprivate fun interface MySam {
fun invoke()
companion object {
operator fun invoke(randomArg: String) = MySam { }
}
}
When invoking MySam("anything")
we get:
Exception in thread "main" java.lang.ClassCastException: MySam$Companion$invoke$1 cannot be cast to kotlin.jvm.functions.Function
UPDATE: workaround is to instead declare as an extension function of the companion object
:
private operator fun MySam.Companion.invoke(randomArg: Int) = MySam { }
UPDATE2: Another workaround is to change interface visibility to non-private, then it works. Is this expected behavior?
Try it out here: https://pl.kotl.in/OxBHwZAn5kqr
03/30/2021, 10:38 AMMark
03/30/2021, 10:39 AMkqr
03/30/2021, 10:57 AMTomasz Krakowiak
03/30/2021, 7:53 PM