问题描述
我想从Kotlin调用重载的Java函数.我想使用 null
作为对重载分辨率有意义的参数.
I want to call a overloaded java-function from Kotlin. I want to use a null
for parameter that significant to a overload resolution.
如何为 null
值指定 IntArray
类型?
我不喜欢带有普通类型附加变量的解决方案.
I don't like a solution with additional varibale of common type.
推荐答案
不是将变量强制转换,而是将 null设置为IntArray?
,例如:
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?)
请注意,这与Java中的行为相同,在Java中,您还需要强制转换 null
,例如(int [])null
,以调用适当的重载方法.
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.
您可以构建一个函数,为您提供〜类型的 null
(如果尚不存在),并使用以下类型:
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>()
但是再说一次,像IntArray一样为空?
我认为很清楚,人们已经知道了.
But then again, null as IntArray?
is clear enough I think and people know it already.
这篇关于Kotlin:为空,并且“过载分辨率模糊度"为"0".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!