Calling Java from Kotlin - type inference for <T extends One & Two>

I recently came across this question on StackOverflow.
To summarize, OP has the following Java method’s signature

<T extends View & ISpecificView> T getSpecificView();

And he’s currently able to call it, in Java, without explicitly specifying a generic type, defaulting to View or ISpecificView, depending on the used methods.

getContainer().getSpecificView().whateverViewMethod();

However, when trying the same thing in Kotlin, a

Type inference failed: Not enough information to infer parameter T

error emerges.
As you can see in the question, I came up with a hackish solution, which is basically to create an abstract class

abstract class KView : View(), ISpecificView

and use it to specify a generic type

val view: View = getContainer().getSpecificView<KView>()

Being my Kotlin knowledge is limited, is there a better solution? I couldn’t find any.

1 Like

Hello. At a glance this looks like https://youtrack.jetbrains.com/issue/KT-15263, which should be fixed in the experimental new inference: New Type Inference in Kotlin 1.3.0-rc-190. Please try it with the latest version of Kotlin. If it doesn’t work for your code example, please submit a new issue at http://kotl.in/issue, maybe we could support this case in the future.