If I instantiate the following object: ```object X...
# getting-started
k
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
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
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
You're right, I didn't test the "positive" case.
Either way, definitely a compiler bug.
k
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