请解释为什么会发生这些错误。谢谢
1.
fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}
fun main() {
val resultAdd1 = calculate(5, 3) { x, y -> x + y } // this worked
println("Result of addition: $resultAdd1")
val resultAdd2 = calculate(5, 3) { (x, y) -> x + y } // Type mismatch: inferred type is (Int) -> Int but (Int, Int) -> Int was expected
println("Result of subtraction: $resultAdd2")
}
{} 内的代码是函数吗?如果是这样,calculate(5, 3) { x, y -> x + y } 是否意味着在函数内部传递函数?
2.
fun greet(action: () -> Unit) {
println("Preparing to greet...")
action()
}
fun main() {
greet {
println("Hello, World!") // this worked
}
greet {
() -> println("Hello, World!") // Type mismatch: inferred type is (Any?) -> Unit but () -> Unit was expected
}
}
(x, y)
是解构语法的一部分。但如果你尝试改变 fun calculate(a: Int, b: Int, operation: (Int, Int) -> Int)
到
fun calculate(a: Int, b: Int, operation: (Int) -> Int)
它也不会工作,即使编译器说它看起来像这样。因为它仍然是不完整的解构语法。
Function0
而不是 lambda:(为此,请单击 Tools
-> Kotlin
-> Show Kotlin Bytecode
-> Decompile
) public static final void greet(@NotNull Function0 action) {
Intrinsics.checkNotNullParameter(action, "action");
String var1 = "Preparing to greet...";
System.out.println(var1);
action.invoke();
}
public static final void main() {
greet((Function0)null.INSTANCE);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}
Function0
是一个函数式接口,它接受 0 个参数并返回一些值。
如果反编译之前的示例 (1),您将看到
Function2
,因为它需要 2 个参数并返回一些内容:
public static final int calculate(int a, int b, @NotNull Function2 operation) {
Intrinsics.checkNotNullParameter(operation, "operation");
return ((Number)operation.invoke(a, b)).intValue();
}
public static final void main() {
int resultAdd1 = calculate(5, 3, (Function2)null.INSTANCE);
String var1 = "Result of addition: " + resultAdd1;
System.out.println(var1);
}
// $FF: synthetic method
public static void main(String[] var0) {
main();
}