Kotlin: why does the compiler need a `const` modifier?

In Kotlin there are:

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

From the documentation :

Compile time constants

Properties whose value is known at compile time can be marked as compile-time constants using a modifier const. Such properties must meet the following requirements:

  • Top level or feature element
  • Initialized by a value of type String or primitive type
  • No custom getter

Given that the kotlin compiler knows to identify initialized values ​​(for example, there is no need to determine the type of a variable in the initializer):

  • Why does the compiler need the help of a programmer?
  • ", " "" const ?
+4
3

const .

, , getter, , .

, , const . , .

+6

:

  • . A) B) . - const, , .
  • . , , - , .

: const - , ... , , ? , - , - ? const, , .

+2

, " " const.

const vals , . vals - getter.

, - Kotlin Intellij Android Studio, object , val const val.

, 200 , , const val.

0

Source: https://habr.com/ru/post/1683500/


All Articles