"overload resolution ambiguity"当函数参数标记为"NonNull"
"overload resolution ambiguity" when function parameter is marked "NonNull"
这是我的 Kotlin(版本 1.3.61)+ Java(1.8.0_201) 编译失败的代码:
专家:
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.1</version>
</dependency>
Test.Kt:
fun main(args: Array<String>) {
Point().setX(1)
}
Point.java:
import javax.annotation.Nonnull;
public class Point {
public void setX(int x) {
}
public void setX(@Nonnull Integer x) {
}
}
由于以下错误而无法编译:
Error:(2, 13) Kotlin: Overload resolution ambiguity:
public open fun setX(@Nonnull x: Int): Unit defined in Point
public open fun setX(x: Int): Unit defined in Point
如果我在第二个 setX
函数中删除 @NonNull
注释,那么这个演示可以编译。我以为 Java 注释只是额外的元数据,不会影响代码本身,所以我不知道为什么会出现歧义。
此外,我 运行 javap -s Point.class
发现它们具有相同的输出:
public void setX(java.lang.Integer);
descriptor: (Ljava/lang/Integer;)V
有人可以帮忙解释一下这是怎么回事吗?提前致谢。
来自 kotlin 实战
sometimes Java code contains information about nullability, expressed
using annotations. When this information is present in the code,
Kotlin uses it. Thus @Nullable String in Java is seen as String? by
Kotlin, and @NotNull String is just String. The interesting
question is what happens when the annotations aren’t present. In that case, the
Java type becomes a platform type in Kotlin.
When you use Java declarations from Kotlin, Java primitive types
become non-null types (not platform types), because they can’t hold
null values.
在你的情况下,第一个方法有一个 java 原始类型作为参数和 因为原始类型不能为 null kotlin将其翻译成
setX(x: Int)
并且由于第二种方法包含@Nonnull 注释,kotlin 使用此信息并将该方法转换为
setX(x: Int)
正如您在 kotlin 中看到的那样,它们是具有完全相同签名的两种不同方法,因此存在歧义。
这是我的 Kotlin(版本 1.3.61)+ Java(1.8.0_201) 编译失败的代码:
专家:
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>2.0.1</version>
</dependency>
Test.Kt:
fun main(args: Array<String>) {
Point().setX(1)
}
Point.java:
import javax.annotation.Nonnull;
public class Point {
public void setX(int x) {
}
public void setX(@Nonnull Integer x) {
}
}
由于以下错误而无法编译:
Error:(2, 13) Kotlin: Overload resolution ambiguity:
public open fun setX(@Nonnull x: Int): Unit defined in Point
public open fun setX(x: Int): Unit defined in Point
如果我在第二个 setX
函数中删除 @NonNull
注释,那么这个演示可以编译。我以为 Java 注释只是额外的元数据,不会影响代码本身,所以我不知道为什么会出现歧义。
此外,我 运行 javap -s Point.class
发现它们具有相同的输出:
public void setX(java.lang.Integer);
descriptor: (Ljava/lang/Integer;)V
有人可以帮忙解释一下这是怎么回事吗?提前致谢。
来自 kotlin 实战
sometimes Java code contains information about nullability, expressed using annotations. When this information is present in the code, Kotlin uses it. Thus @Nullable String in Java is seen as String? by Kotlin, and @NotNull String is just String. The interesting question is what happens when the annotations aren’t present. In that case, the Java type becomes a platform type in Kotlin.
When you use Java declarations from Kotlin, Java primitive types become non-null types (not platform types), because they can’t hold null values.
在你的情况下,第一个方法有一个 java 原始类型作为参数和 因为原始类型不能为 null kotlin将其翻译成
setX(x: Int)
并且由于第二种方法包含@Nonnull 注释,kotlin 使用此信息并将该方法转换为
setX(x: Int)
正如您在 kotlin 中看到的那样,它们是具有完全相同签名的两种不同方法,因此存在歧义。