尝试编译 Java 时 @FunctionalInterface
具有超过 1 个非抽象方法会引发编译错误。
但是,在 Kotlin 中执行相同操作时,不会引发错误或警告,即以下 Kotlin interface
编译成功:
@FunctionalInterface
interface Foo {
fun foo()
fun foo(params: Map<String, String>)
}
这是 Kotlin 编译器中的预期行为还是错误?
请注意,为上述 Kotlin 片段生成的字节码等同于以下 Java 片段(正确地 - 无法编译):
@FunctionalInterface
// metadata omitted
public interface Foo {
void foo();
void foo(@NotNull Map var1);
}
最佳答案
问题 KT-25512已提交给 JetBrains 的问题跟踪器(由另一个用户)报告编译器在 @FunctionalInterface
时出现异常的事实。应用于非 SAM 界面,截至 2019 年 2 月 10 日,该问题仍处于打开状态,没有任何事件。
关于Kotlin @FunctionalInterface 使用多种抽象方法进行编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54514692/