Ellen Spertus
09/16/2022, 3:22 AMconst
keyword necessary? I understand that const val
is for compile-time constants and leads to different (presumably better) code than val
, but why couldn't the compiler generate the better code if a [static] val
is assigned a compile-time constant?
In other words, why does the compiler generate different code for these two lines?
const val PI = 3.1415
val PI = 3.1415
ephemient
09/16/2022, 3:59 AMephemient
09/16/2022, 4:00 AMephemient
09/16/2022, 4:03 AMconst val FOO = 1
, somebody else builds a library using FOO
, and then I release a new version with FOO = 2
, a consumer of both libraries will see mismatched valuesephemient
09/16/2022, 4:05 AMval
does not change the API and dependents do not need to be recompiled)ephemient
09/16/2022, 4:07 AMconst
- they're necessary for use in annotations, for example, which also explains why they are inlined - but it is a significant decision to make regarding your APIephemient
09/16/2022, 4:17 AMpublic class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
in order to create a static final but non-constant fieldsreich
09/26/2022, 6:18 PMsreich
09/26/2022, 6:19 PMephemient
09/26/2022, 6:21 PMsreich
09/26/2022, 6:21 PMsreich
09/26/2022, 6:22 PMephemient
09/26/2022, 6:22 PMsreich
09/26/2022, 6:23 PMNorbi
09/29/2022, 8:54 PMNorbi
09/29/2022, 8:56 PM