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.