Confused why this doesn’t work: ```private fun int...
# announcements
m
Confused why this doesn’t work:
Copy code
private fun interface MySam {
    fun invoke()
    
    companion object {
        operator fun invoke(randomArg: String) = MySam { }
    }
}
When invoking
MySam("anything")
we get:
Copy code
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
:
Copy code
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/OxBHwZAn5
k
is the last parameter lambda?
m
Not sure what you mean
k
ignore me
t
👍 1