https://kotlinlang.org logo
#getting-started
Title
# getting-started
v

Vitali Plagov

04/26/2022, 9:31 AM
I have the following code:
Copy code
object TestConfig {
  val ENV_UNDER_TEST by lazy { ... }
}
It’s a global variable I’m using in the project that stores the environment name under test. Is it possible to change that variable during the runtime? maybe with reflection or somehow else? I do understand that it’s not supposed to be changed and that it’s a dirty hack, so I accept that risk.
r

Ron S

04/26/2022, 12:04 PM
"val" stands for immutability. You could use property delegation, however, and override the getValue-Operator which might return the value of a mutable backing field. But if TestConfig can be modified why not make it a data class and use .copy it if needed?
👍 1
5 Views