为什么Java方法的Integer参数映射到Int而不是平台类型? [英] Why is Integer parameter of Java method mapped to Int and not platform type?
问题描述
受另一个问题。
在 fastutil
库有 IntArrayList
类,它有一个带有以下Java签名的方法:
In fastutil
library there's IntArrayList
class which has a method with the following Java signature:
public void push(Integer o)
来自Kotlin它被视为
From Kotlin it is seen as
push(o: Int)
具体原因是 Int
而不是平台类型 Int!
?
Is there a specific reason why it is Int
and not platform type Int!
?
我希望它是 push(o:Int!)
至少因为一个方法使用Kotlin源在项目中的Java源代码中定义的相同签名具有 Int!
作为从Kotlin看到的参数类型(甚至在不同模块中定义,甚至从该模块的jar中导入) !)。
I expected it to be push(o: Int!)
at least because a method with the same signature defined in Java source within the project with Kotlin sources has Int!
as parameter type seen from Kotlin (even defined in different module, and even imported from jar of that module!).
此外,描述d行为导致 push(整数o)
与 push(int o)
(在同一个类中)有冲突合法地 Int
参数 - 它们都被视为 push(o:Int)
。如果对于
整数
有 Int!
,则不存在冲突(我也尝试在这里定义这对方法)我的代码 - 仍然按照我的预期工作,有 Int!
)。
Also, the described behavior causes push(Integer o)
to conflict with push(int o)
(in the same class) which has Int
parameter legally -- they are both seen as push(o: Int)
. If there was Int!
for Integer
, there would be no conflict (I tried also to define this pair of methods in my code -- still works as I expect, there's Int!
).
Kotlin版本 1.0.2
。
的gradle依赖性fastutil
:
compile group: 'it.unimi.dsi', name: 'fastutil', version: '7.0.12'
推荐答案
我怀疑它是一个kotlin编译器错误。
这里的问题在接口 IntStack
中由 IntArrayList
实现:
I suspect that it is a kotlin compiler bug.
Problem here is in interface IntStack
which is implemented by IntArrayList
:
interface Stack<T> {
void push(T t);
}
interface IntStack extends Stack<Integer> {
void push(int i);
}
Kotlin只看到一种方法 push(Int)
在接口 IntStack
中,或者更确切地说,kotlin错误地认为 push(int i)
是方法的重写 push(T t)
。
Kotlin sees only one method push(Int)
in interface IntStack
, or, more precisely, kotlin wrongly supposes that push(int i)
is an override of method push(T t)
.
这篇关于为什么Java方法的Integer参数映射到Int而不是平台类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!