Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Overload Resolution Ambiguity in Kotlin (no lambda)?

I am having Overload Resolution Ambiguity error in this line:

departureHourChoice!!.selectionModel.select(currentHourIndex)

For Reference:

  • departureHourChoice is a ChoiceBox<Int>, which is from java.scene.control

  • currentHourIndex is an Int

  • The Overload Resolution Ambiguity happens in the .select() method; It is overloaded and can accept two kinds of parameters: (T obj) or (int index).

  • The .select() method allows for an item in a ChoiceBox to be selected, and you can determine which one can be selected by referencing to that item or to it's index. In this case, I want it to be selected by Index (int).

  • Here is a photo of the errorenter image description here

How would one resolve the Overload Resolution Ambiguity?

like image 427
Berry Avatar asked Aug 05 '16 01:08

Berry


1 Answers

It seems that you are hit by this bug as a workaround you can :

  • box the currentHourIndex:

    lateinit var departureHourChoice: ChoiceBox<Int>
    ...
    val currentHourIndex = 1
    departureHourChoice.selectionModel.select(currentHourIndex as Int?)
    
  • or change declaration of ChoiceBox to use java.lang.Integer instead of Kotlin's Int:

    lateinit var departureHourChoice: ChoiceBox<java.lang.Integer>
    ...
    val currentHourIndex = 1
    departureHourChoice.selectionModel.select(currentHourIndex)
    

Further reading:

  • Why is Integer parameter of Java method mapped to Int and not platform type?
  • Kotlin: What can I do when a Java library has an overload of both primitive and boxed type?
like image 133
miensol Avatar answered Sep 21 '22 14:09

miensol