Brad Harris
10/11/2021, 11:27 PMJoffrey
10/11/2021, 11:39 PMconst defines a compile-time constant. This means the value is inlined at compile time in every usage site.ephemient
10/11/2021, 11:40 PMconst is a compile-time constant, which only works for specific types and can be used in annotations. on JVM, it is a static final that the Java language will inline into callsites.
a non-const val 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.Joffrey
10/11/2021, 11:41 PMconst has an important consequence: if module A uses a const val 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.ephemient
10/11/2021, 11:43 PMstatic final 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)gildor
10/12/2021, 4:50 AMval myVariable get() = "const"gildor
10/12/2021, 4:51 AMconst val and it doesn’t create unnecessary field to keep constant value, only getter which returns valueCLOVIS
10/20/2021, 6:57 AMgildor
10/20/2021, 6:58 AMephemient
10/20/2021, 10:25 PMconst (and Java can't use non-const in switch 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