Kelvin Chung
07/24/2025, 4:36 AMinterface MyInterface {
val value: Int
}
object MyObject : MyInterface {
override val value = 1
}
The question I have is why in this specific scenario (implementing an interface in an object) you cannot mix const
and override
together, and you have to do so through some kind of middleman. Any insight as to why this would be the case?Pablichjenkov
07/24/2025, 4:59 AMconst override
or final override
, i'd guess it is because the object is final already so you don't need to specify it. No body will override it anyway.CLOVIS
07/24/2025, 8:26 AMconst
means the value is inlined everywhere at compile-time. If there's an interface, that's not possible (because at compile-time we can't know which implementation is used). This is the same reason why value class
are boxed when used as an interface.
It doesn't really matter though, const
doesn't make a big performance difference, especially if the value is just 1
. For example, the JVM is able to inline constants itself at runtime even if they are not marked with const
.
So I'd say, just leave it like this, it's not going to be an issue.