https://kotlinlang.org logo
#getting-started
Title
# getting-started
k

Klitos Kyriacou

11/30/2023, 5:12 PM
If I instantiate the following object:
Copy code
object X : AbstractMap<Int, Int>() {
    override val entries: Set<Map.Entry<Int, Int>> = object : AbstractSet<Map.Entry<Int, Int>>() {
        override val size = 0
        override fun iterator() = emptySet<Map.Entry<Int, Int>>().iterator()
    }
}
then everything works fine. However, if I omit the explicit type specification, like this:
Copy code
object X : AbstractMap<Int, Int>() {
    override val entries = object : AbstractSet<Map.Entry<Int, Int>>() {
        override val size = 0
        override fun iterator() = emptySet<Map.Entry<Int, Int>>().iterator()
    }
}
then everything still compiles just fine, but at runtime I get this error:
Copy code
java.lang.AbstractMethodError: Receiver class X does not define or inherit an implementation of the resolved method 'abstract java.util.Set getEntries()' of abstract class kotlin.collections.AbstractMap.
	at kotlin.collections.AbstractMap.entrySet(AbstractMap.kt:21)
Playground I was surprised to see this because this error is documented as:
Normally, this error is caught by the compiler; this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.
But in my case this error has occurred without any change in the class.
d

Daniel Pitts

11/30/2023, 5:35 PM
I suspect this is a bug in the compiler, since the second example shouldn't have actually compiled, since
entries
type is wrong, and therefore doesn't override the Map.getEntries() method.
If I replace the
override val entries
with
override fun getEntries()
, then the compiler gives the expected output. (playground)
k

Klitos Kyriacou

11/30/2023, 5:47 PM
No, that's a totally different thing. If you add the type spec (
override fun getEntries(): Set<Map.Entry<Int, Int>>
), it still gives you the error. It also says that
getEntries
overrides nothing. You can't override a
val
with a getter function.
d

Daniel Pitts

11/30/2023, 5:48 PM
You're right, I didn't test the "positive" case.
Either way, definitely a compiler bug.
k

Klitos Kyriacou

11/30/2023, 5:56 PM
Thanks, indeed it is. I found it's already been reported 6 years ago (and still unresolved!) https://youtrack.jetbrains.com/issue/KT-20070/Overriding-Map.entries-with-a-subtype-to-Set-caused-a-runtime-error