Android overload resolution ambiguity all these functions match

Overload resolution ambiguity. All these functions match

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?

4 Answers

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:

The issue is that you have a property called abc , which can be accessed by calling the Kotlin property accessor this.abc (sort of «getter») and you define another method with the same name, so it doesn’t recognize which one to reference with syntax ::abc .

This is also stated by the error message from the IDE:

Overload resolution ambiguity. All these functions match:

  • private final lateinit var abc: ABC defined in MyClass
  • public final fun abc(): Unit defined in MyClass

If you would access the function abc or the property abc on an instance of MyClass without the reference syntax, it would actually work, although I would not recommend it. See here:

This code compiles without any problems.

But this is not what you want. Create a backing field _abc and access it via a property without backing field abc . This way you can return a default instance of ABC , if _abc has not been initialized.

You can disambiguate the reference by extracting a variable and explicitly specifying its type:

Читайте также:  Как удалить гугл календарь андроид

Источник

Overload resolution ambiguity. All these functions match

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?

4 Answers

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:

The issue is that you have a property called abc , which can be accessed by calling the Kotlin property accessor this.abc (sort of «getter») and you define another method with the same name, so it doesn’t recognize which one to reference with syntax ::abc .

This is also stated by the error message from the IDE:

Overload resolution ambiguity. All these functions match:

  • private final lateinit var abc: ABC defined in MyClass
  • public final fun abc(): Unit defined in MyClass

If you would access the function abc or the property abc on an instance of MyClass without the reference syntax, it would actually work, although I would not recommend it. See here:

This code compiles without any problems.

But this is not what you want. Create a backing field _abc and access it via a property without backing field abc . This way you can return a default instance of ABC , if _abc has not been initialized.

You can disambiguate the reference by extracting a variable and explicitly specifying its type:

Источник

Оцените статью