【发布时间】:2019-08-12 05:32:25
【问题描述】:
我有以下函数来模拟kotlin的三元运算符
fun Boolean.then(action: () -> Unit): Boolean {
if (this)
action.invoke()
return this
}
fun Boolean.otherwise(action: () -> Unit) {
if (!this)
action.invoke()
}
fun <T> Boolean.then(func: () -> T): T? {
if (this)
return func.invoke()
return null
}
fun <T> T?.otherwise(action: () -> T): T {
return this ?: action.invoke()
}
它们应该像这样使用:
(check).then { doHello() }.otherwise { doWorld() }
val answer = (check).then { "hello" }.otherwise { "world" }
但是,当我尝试使用上述运算符分配值时:
val visibility: Int = (show).then { View.VISIBLE }.alt { View.GONE }
我收到一条错误消息,说所需的回复是 Int,但它实际上得到了 Unit,这意味着它调用了方法的第一个版本而不是第二个版本
除了重命名方法(当我将前两个更改为 thenDo 否则它是否有效)之外,我是否可以以某种方式编写上述代码,以便编译器知道调用第二个版本?
【问题讨论】:
-
val visibility = if (show) View.VISIBLE else View.GONE有什么问题? -
或者如果您的情况实际上更复杂:
yourObject.takeIf { /* complex condition */ }?.let { View.VISIBLE /* or the transformation if condition holds */ } ?: View.GONE /* default value if condition doesn't hold */ -
val 可见性:Int = (show).then
{ View.VISIBLE }.alt { View.GONE } -
如果
then的块在某些情况下返回空值,你的代码会编译,但可能不是你想要的。 -
我对您在这里尝试做的事情感到非常不舒服,这基本上是在 Kotlin 之上构建您自己的语言。这对 DSL 来说很好,但 Kotlin 的
if表达式已经完全符合您的要求。您的方法使其他开发人员、IDE 和其他工具以及您阅读其他人的代码(您肯定必须这样做)更难理解。
标签: kotlin