Kotlin: why the compiler needs the `const` modifier?

Viewed 661

In Kotlin there are:

  • val - readonly property
  • const val - compile-time constants

From the documentation:

Compile-Time Constants

Properties the value of which is known at compile time can be marked as compile time constants using the const modifier. Such properties need to fulfill the following requirements:

  • Top-level or member of an object
  • Initialized with a value of type String or a primitive type
  • No custom getter

Given that kotlin compiler does know to identify initialized values (such as there is no need defining the variable type in an initializer):

  • Why does the compiler needs the programmer's assistance?
  • Can't it identify "properties the value of which is known at compile time" and "add" the const modifier by itself?
3 Answers
Related