Brad Harris10/11/2021, 11:27 PM
Joffrey10/11/2021, 11:39 PM
defines a compile-time constant. This means the value is inlined at compile time in every usage site.
ephemient10/11/2021, 11:40 PM
is a compile-time constant, which only works for specific types and can be used in annotations. on JVM, it is a
that the Java language will inline into callsites. a non-
is a read-only property and its containing object must be initialized to set its value. callsites are not inlined (which is slightly better for incremental compilation) and it cannot be used in annotations.
Joffrey10/11/2021, 11:41 PM
has an important consequence: if module A uses a
declared in module B, and the value of the constant is later changed in module B, then module A needs to be recompiled against the new version of module B to see the change. Otherwise, A would still use the old constant value, even if the new module B is on the classpath.
ephemient10/11/2021, 11:43 PM
fields may or may not be compile-time constants depending on their initializer; Kotlin clearly separates the two, which is IMO a strict win (it has ABI compatibility implications)
gildor10/12/2021, 4:50 AM
val myVariable get() = "const"
and it doesn’t create unnecessary field to keep constant value, only getter which returns value
CLOVIS10/20/2021, 6:57 AM
gildor10/20/2021, 6:58 AM
ephemient10/20/2021, 10:25 PM
(and Java can't use non-
but that's not an issue for Kotlin/`when`) otherwise yeah, a simple getter is easily optimized away, and I like it over a val with constant backing field