In my activity I have a field that should be non-nullable and has a custom setter. I want to initialize the field in my onCreate
method so I added lateinit
to my variable declaration. But, apparently you cannot do that (at the moment): https://discuss.kotlinlang.org/t/lateinit-modifier-is-not-allowed-on-custom-setter/1999.
These are the workarounds I can see:
Can someone recommend a better way (that does not involve removing the custom setter)?
If we don't initialize a lateinit variable before using it gives an error of “lateinit property has not been initialized”. You can check if the lateinit variable has been initialized or not before using it with the help of isInitialized() method.
Use lateinit for properties that cannot be initialized in a constructor. Don't you mean usse lateinit for properties that can be initialized in the constructor. If you use lateinit and don't initialize it a constructor then you run the risk of accessing it before its been initialized resulting in a exception.
lateinit var is only for first time initialization, for cases when you know that your variable is not nullable but you cannot initialize it in constructor(for example in android framework). There is no point in having lateinit var if you "uninitialize" it after. Kotlin does not allow this kind of manipulation.
This is because whenever a lateinit property is accessed, Kotlin provides it a null value under the hood to indicate that the property has not been initialized yet.
Replace it with a property backed by nullable property:
private var _tmp: String? = null
var tmp: String
get() = _tmp!!
set(value) {_tmp=value; println("tmp set to $value")}
Or this way, if you want it to be consistent with lateinit
semantics:
private var _tmp: String? = null
var tmp: String
get() = _tmp ?: throw UninitializedPropertyAccessException("\"tmp\" was queried before being initialized")
set(value) {_tmp=value; println("tmp set to $value")}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With