如何将函数输出转换为Kotlin单元 [英] How to convert function output to Unit with Kotlin
问题描述
我在Kotlin中遇到了应该返回Unit的函数的麻烦,但是由于使用了另一个返回布尔值的函数,所以会出现类型不匹配的情况。
是一个人为的例子:
fun printAndReturnTrue(bar:Int):Boolean {
println(bar)
返回true
fun foo(bar:Int):Unit = when(bar){
0 - > println(0)
else - > printAndReturnTrue(bar)
}
在这里,我其实并不关心 code> printAndReturnTrue 返回一个布尔值。我只想让富美执行副作用操作。但编译器会警告类型不匹配:我的 else
应该返回一个 Unit
值。
有没有一种很好的方法将值转换为 Unit
?
最简单的我看到的解决方案是:
fun foo(bar:Int):Unit = when(bar){
0 - > println(0)
else - > {
printAndReturnTrue(bar)
Unit
}
}
或:
pre $ fun foo(bar:Int):Unit = when(bar){
0 - > println(0)
else - > eraseReturnValue(printAndReturnTrue(bar))
}
fun eraseReturnValue(value:Any)=单位
或者我使用全功能表单:
fun foo(bar:Int):Unit {
when(bar){
0 - > println(0)
else - > printAndReturnTrue(bar)
}
}
我相信有一些惯用的(或者是最后一个例子?),但现在我没有找到它们。
不幸的是,没有习惯的方式来做到这一点。一个类似的问题,传递一个(T)类型的lambda - > Boolean
转换为接受(T) - >单元
lambdas之前就已经出现过了,那里需要的自动转换应该会在未来的某个时候出现。
现在,当表达式将它强制回到 Unit
值时,可以在结尾处使用扩展函数:
fun任何?.toUnit()=单位
fun foo(bar:Int):Unit = when (bar){
0 - > println(0)
else - > printAndReturnTrue(bar)
} .toUnit()
另外,如果你有扩展属性碰巧更好:
val任何?.unit get()=单位
fun foo (bar:Int):Unit = when(bar){
0 - > println(0)
else - > printAndReturnTrue(bar)
} .unit
当然,您可以省略 Unit
函数的返回类型,如果使用其中任何一个。
I have troubles with a function in Kotlin that should return Unit, but due to a usage of another function returning a Boolean, there is a type mismatch.
Here is a contrived example:
fun printAndReturnTrue(bar: Int): Boolean {
println(bar)
return true
}
fun foo(bar: Int): Unit = when(bar) {
0 -> println("0")
else -> printAndReturnTrue(bar)
}
Here, I actually do not care about the fact that printAndReturnTrue
returns a boolean. I just want foo to perform side-effect operations. But the compiler warns about a type mismatch: my else
should return a Unit
value.
Is there a nice way to convert a value to Unit
?
The simplest solutions I see are:
fun foo(bar: Int): Unit = when(bar) {
0 -> println("0")
else -> {
printAndReturnTrue(bar)
Unit
}
}
or:
fun foo(bar: Int): Unit = when(bar) {
0 -> println("0")
else -> eraseReturnValue(printAndReturnTrue(bar))
}
fun eraseReturnValue(value: Any) = Unit
Or I use the full function form:
fun foo(bar: Int): Unit {
when(bar) {
0 -> println("0")
else -> printAndReturnTrue(bar)
}
}
I am sure there are some idiomatic ways to do that (or is is the last example?), but for now I did not find them.
Unfortunaly, there's no idiomatic way to do this. A similiar problem, passing a lambda of type (T) -> Boolean
to a function accepting (T) -> Unit
lambdas has come up before, and the auto conversion required there is supposed to come to the language at some future point.
For now, you could use an extension function at the end of the when
expression to coerce it back into a Unit
value:
fun Any?.toUnit() = Unit
fun foo(bar: Int): Unit = when(bar) {
0 -> println("0")
else -> printAndReturnTrue(bar)
}.toUnit()
Alternatively, an extension property, if you happen to like that better:
val Any?.unit get() = Unit
fun foo(bar: Int): Unit = when(bar) {
0 -> println("0")
else -> printAndReturnTrue(bar)
}.unit
Of course you can omit the explicit Unit
return type of the function if you use either of these.
这篇关于如何将函数输出转换为Kotlin单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!