Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin: null and "Overload resolution ambiguity"

I want to call a overloaded java-function from Kotlin. I want to use a null for parameter that significant to a overload resolution.

How to specify IntArray type for null value?

I don't like a solution with additional varibale of common type.

enter image description here

like image 769
mazzy Avatar asked Aug 07 '18 08:08

mazzy


1 Answers

Instead of a variable just cast it, i.e. null as IntArray?, e.g.:

origImg.data.getSamples(0, 0, origImg.width, origImg.height, 0, null as IntArray?)

Note that this is the same behaviour as in Java, where you also needed to cast null, e.g. (int[]) null, to call the appropriate overloaded method.

You could build a function that gives you a ~typed null (if it doesn't exist yet) with a reified type:

inline fun <reified T>typedNull(): T? = null

and calling it with:

typedNull<IntArray>()

But then again, null as IntArray? is clear enough I think and people know it already.

like image 59
Roland Avatar answered Nov 15 '22 23:11

Roland