Klitos Kyriacou
11/30/2023, 5:12 PMobject 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:
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:
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.Daniel Pitts
11/30/2023, 5:35 PMentries
type is wrong, and therefore doesn't override the Map.getEntries() method.Daniel Pitts
11/30/2023, 5:37 PMoverride val entries
with override fun getEntries()
, then the compiler gives the expected output. (playground)Klitos Kyriacou
11/30/2023, 5:47 PMoverride 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.Daniel Pitts
11/30/2023, 5:48 PMDaniel Pitts
11/30/2023, 5:49 PMKlitos Kyriacou
11/30/2023, 5:56 PM