I tried to check, lateinit variable is initialized or not in a function using reference operator. The function name and variable name is same in this case. So that Kotlin throws
Overload resolution ambiguity. All these functions match
exception. Actually what's wrong in this code?
class ABC
class MyClass {
private lateinit var abc: ABC
fun abc() {
if(!::abc.isInitialized){
println("Hello")
}
}
}
You can disambiguate the reference by extracting a variable and explicitly specifying its type:
class ABC
class MyClass {
private lateinit var abc: ABC
fun abc() {
val abc: KProperty0<ABC> = ::abc
if(!abc.isInitialized){
println("Hello")
}
}
}
It's a simple name clash. The compiler does not know whether you're referring to the method abc
or the property abc
. A renaming fixes the problem:
class MyClass {
private lateinit var abc: ABC
fun abcFun() {
val prop = this::abc
if (!::abc.isInitialized) {
println("Hello")
}
}
}
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