0

Is there any way to know Val's value is known at compile-time or at runtime? I mean can I check that at IDE or somewhere? Since after I google it, there is no article talk about it. They just point out which is known at compile and which is known at runtime, but don't mention how to know that.

As far as I know, if I assign a primitive type or String to Val, its value will be known at compile time. Instead, if I assign a function or object(the value needs to be computed or reference from another place), its value will be known at runtime. But is there any way to verify that or to know that the value we assigned will be known at compile time or runtime(through IDE or decompile)?

  • 1
    why do you need that? – IR42 Oct 08 '21 at 14:38
  • 1
    I don't think I understand your question. The values are assigned at runtime unless the `const` keyword is used. For instance, a property such as `val x: Int = 42` inside a class, will only be initialized when the class is instantiated (at runtime). Literals are of course part of the byte code somewhere, but is that what you mean by "assigned at compile time"? Or are you talking about how to know whether a literal can be assigned to a `const val`? – Joffrey Oct 08 '21 at 14:50
  • *if I assign a primitive type or String to Val, its value will be known at compile time* - in which respect? For which purpose? Some (not all) variables initialized with integer literals can be understood by the compiler, which can then optimize away useless `if`s or things like that, but that's an implementation detail. Is that what you would like to know? I wonder what you want to do with this information if you have it. You could read the bytecode, but the JIT could make any analysis moot. – Joffrey Oct 08 '21 at 15:00
  • Just out of curiosity. For me, this information is just like some knowledge from the textbook. But how do you know this information is right? By the way, this answer https://stackoverflow.com/questions/37595936/what-is-the-difference-between-const-and-val says that "consts are compile time constants. Meaning that their value has to be assigned during compile time, unlike vals, where it can be done at runtime." So base on "can be done at runtime", I think it can also be done at compile time. – Chia Hao Yu Oct 09 '21 at 03:54

1 Answers1

1

Use a const val.

If you declare a val with the additional modifier const, the compiler will make sure that it is a compile-time constant.

const val myCompileTimeConstant = "Hello, World!"

https://kotlinlang.org/docs/properties.html#compile-time-constants

You can, at runtime, use reflection to check if a property was declared as const:

val isCompileTimeConstant = ::myCompileTimeConstant.isConst //true
Sam
  • 8,330
  • 2
  • 26
  • 51
  • So how do you know the compiler will make sure that it is a compile-time constant? Is there any way to verify that except for the document saying that? – Chia Hao Yu Oct 09 '21 at 03:41
  • @ChiaHaoYu, if official docs and [language specification](https://kotlinlang.org/spec/declarations.html#constant-properties) is not enough for you, then you can always check out [source code of the compiler](https://github.com/JetBrains/kotlin/blob/92d200e093c693b3c06e53a39e0b0973b84c7ec5/compiler/frontend/src/org/jetbrains/kotlin/resolve/checkers/ConstModifierChecker.kt#L75-L81) - it's open-sourced and hosted on Github. – Михаил Нафталь Oct 09 '21 at 08:30