https://kotlinlang.org logo
#announcements
Title
# announcements
a

arild

08/23/2019, 10:01 AM
Are there any cases where a
val
might be
const
but it is useful not to make it
const
?
1
d

diesieben07

08/23/2019, 10:05 AM
const
means the value of the variable will be inlined everywhere it's used. So if you can foresee ever changing the value you should not make it
const
.
h

hugo.matilla

08/23/2019, 10:27 AM
I don’t get the answer. `val`s can not change.
d

Dominaezzz

08/23/2019, 10:28 AM
He means at compile time.
👍 1
a

arild

08/23/2019, 10:46 AM
@diesieben07 but given that it can only be
const
if it is assigned a literal value and is either top-level, or a member of an object declaration, any change I would make to it would only happen in the context of recompiling anyway, which means the new value would be inlined wherever the const was used. Is there a case when using reflection, perhaps? Or some manner of hot module reloading?
d

Dominaezzz

08/23/2019, 10:50 AM
If library B and C uses library A (which has a const defined). A changes its const's value and publishes update. C upgrades it's used version of A. (B does not). Application D uses libraries B and C. Library B would still have the old value of A's const inlined and C would have the new one.
👍 4
a

arild

08/23/2019, 1:06 PM
Lovely explanation, thank you
3 Views