Hi!
I have a simple question but I do not know whether there is an answer.
When defining an anonymous class you can let the object inherit from multiple interfaces. The object has then both types like every (non anonymous) class that inherits from more then one interface.
interface First { fun runFirst() } interface Second { fun runSecond() } fun test() { val both = object : First, Second { override fun runFirst() { println("first") } override fun runSecond() { println("second") } } // MainKt$test$both$1 println(both.javaClass.typeName) // first // second both.runFirst() both.runSecond() }
My variable both
in the given example is implicitly typed. But what is the Type? IntelliJ says <anonymous object: First, Second>
. The JVM type name seems to be MainKt$test$both$1
. The obvious way to solve this problem is to introduce a new interface:
interface Both: First, Second
But then I could simply make it a non anonymous class instead of an interface.
Is there even a way to explicitly declare “anonymous types”? I am excited to hear your thoughts on this.
submitted by /u/mtorials
[link] [comments]