[英]Anonymous Functions: Regular call VS Shorthand Syntax
I'm new to Kotlin.我是 Kotlin 的新用户。
I was experimenting with Anonymous functions a little bit until facing different outputs for the same concept using different approaches.我对匿名函数进行了一些试验,直到使用不同的方法面对同一概念的不同输出。
var greetingFunction = { playerName: String , numBuildings: Int ->
val currentYear = 2022
println("Adding $numBuildings houses")
"Welcome to SimVillage, $playerName! (Copyright $currentYear)\n"
}
private fun runSimulation(playerName: String, greetingFunc: (String, Int) -> String){
val numOfBuildings = (1..3).shuffled().last()
println(greetingFunc(playerName, numOfBuildings))
}
A- Regular call for Anonymous function: A- 定期拨打匿名电话 function:
println(runSimulation("Ahmed", greetingFunction))
Output: Output:
Adding 3 houses
Welcome to SimVillage, Ahmed! (Copyright 2022)
B- Shorthand call for Anonymous function: B- 匿名拨打电话 function:
println(runSimulation("Different") { playerName: String , numBuildings: Int ->
val currentYear = 2022
println("Adding $numBuildings houses")
"Welcome to SimVillage, $playerName! (Copyright $currentYear)\n"
})
Output: Output:
kotlin.Unit
Adding 2 houses
Welcome to SimVillage, Different! (Copyright 2022)
kotlin.Unit
I tried to remove the println()
and calling the runSimulation function directly and the output was:我试图删除
println()
并直接调用runSimulation function,而 output 是:
Output: Output:
kotlin.Unit
Adding 2 houses
Welcome to SimVillage, Different! (Copyright 2022)
What I really want to know is: how in the first place did I get that "kotlin.Unit" print using the Shorthand Syntax?我真正想知道的是:首先我是如何使用速记语法打印“kotlin.Unit”的?
Kotlin will automatically infer the type of anonymous functions . Kotlin 会自动推断匿名函数的类型。 Since the last line of
greetingFunction
is a String由于
greetingFunction
的最后一行是一个字符串
var greetingFunction = { playerName: String , numBuildings: Int ->
val currentYear = 2022
println("Adding $numBuildings houses")
// the last line is a string -> Kotlin will infer that the return type is a String
"Welcome to SimVillage, $playerName! (Copyright $currentYear)\n"
}
the inferred type is推断的类型是
var greetingFunction: (String, String) -> String
Return types for block-body functions are not inferred . 不推断块体函数的返回类型。 If a function does not return a useful value, its return type is
Unit
. 如果 function 没有返回有用的值,则其返回类型为
Unit
。 The function function
private fun runSimulation(playerName: String, greetingFunc: (String, Int) -> String) {
val numOfBuildings = (1..3).shuffled().last()
println(greetingFunc(playerName, numOfBuildings))
}
will therefore return Unit
, so因此将返回
Unit
,所以
println(runSimulation("foo") { _, _ -> "bar" })
will print the returned value of runSimulation()
, and Unit.toString()
is kotlin.Unit
会打印
runSimulation()
的返回值, Unit.toString()
为kotlin.Unit
Since runSimulation()
will also print to stdout, this is effectively the same as running由于
runSimulation()
也会打印到标准输出,这实际上与运行相同
println(println("bar"))
First the 'inner' println()
will output bar
, and then the 'outer' println()
will print kotlin.Unit
.首先,“内部”
println()
将打印 output bar
,然后“外部” println()
将打印kotlin.Unit
。
bar
kotlin.Unit
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.