Question: Suppose I have this: ```interface MyInte...
# getting-started
k
Question: Suppose I have this:
Copy code
interface 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?
p
I guess you mean using
const 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.
c
const
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.
👍 1