为什么 Java 方法的 Integer 参数映射到 Int 而不是平台类型? [英] Why is Integer parameter of Java method mapped to Int and not platform type?

查看:31
本文介绍了为什么 Java 方法的 Integer 参数映射到 Int 而不是平台类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来自 另一个问题.

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!).

此外,所描述的行为导致 push(Integer o) 与具有 Intpush(int o)(在同一类中)发生冲突code> 参数合法——它们都被视为 push(o: Int).如果IntegerInt!,就不会有冲突(我也尝试在我的代码中定义这对方法——仍然按我的预期工作,有<代码>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.

Kotlin version is 1.0.2.

fastutil 的 Gradle 依赖:

Gradle dependency for 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 在接口 IntStack 中只看到一个方法 push(Int),或者,更准确地说,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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆