在Kotlin中,与这个表达式等价的是什么?
a ? b : c
这在Kotlin中是无效的代码。
在Kotlin中,与这个表达式等价的是什么?
a ? b : c
这在Kotlin中是无效的代码。
当前回答
如果condition为false则为" error ",否则为"someString"
在编写三元条件运算符之前,让我们考虑以下原型:
if (!answer.isSuccessful()) {
result = "wrong"
} else {
result = answer.body().string()
}
return result
解决方案
你可以用!(逻辑上不是)Kotlin if-expression中的运算符:
return if (!answer.isSuccessful()) "wrong" else answer.body().string()
如果你使用if-expression (expression without !操作符):
return if (answer.isSuccessful()) answer.body().string() else "wrong"
Kotlin 's Elvis operator ?:可以做得更好:
return answer.body()?.string() ?: "wrong"
同样,为相应的Answer类使用扩展函数:
fun Answer.bodyOrNull(): Body? = if (isSuccessful()) body() else null
在扩展函数中,由于Elvis操作符,您可以减少代码:
return answer.bodyOrNull()?.string() ?: "wrong"
或者直接用when条件表达式:
when (!answer.isSuccessful()) {
parseInt(str) -> result = "wrong"
else -> result = answer.body().string()
}
其他回答
在Kotlin中,if是一个表达式,即它返回一个值。因此 没有三元操作符(条件?然后:else),因为 普通的如果在这个角色中表现得很好。这里的手动源代码
// Traditional usage
var max = a
if (a < b) max = b
// With else
var max: Int
if (a > b) {
max = a
} else {
max = b
}
// As expression
val max = if (a > b) a else b
正如德鲁·诺克斯引用的,kotlin使用if语句作为表达式, 所以三元条件运算符不再是必要的,
但是使用扩展函数和中缀重载,你可以自己实现,这里有一个例子
infix fun <T> Boolean.then(value: T?) = TernaryExpression(this, value)
class TernaryExpression<out T>(val flag: Boolean, val truly: T?) {
infix fun <T> or(falsy: T?) = if (flag) truly else falsy
}
然后像这样使用它
val grade = 90
val clazz = (grade > 80) then "A" or "B"
在Kotlin中没有三元运算符。乍一看,这似乎有问题。但是我们可以用内联if else语句因为这是表达式。我们要做的就是
var number = if(n>0) "Positive" else "Negetive"
这里我们可以用else if来阻塞我们需要的太多。像- - - - - -
var number = if(n>0) "Positive" else if(n<0) "Negative" else "Zero"
这一行比三元运算符简单易读。当我们在Java中使用多个三元运算符时,这看起来很可怕。但是这里我们有一个清晰的语法。甚至我们也可以把它写成多行。
在Kotlin中没有三元操作,但是有一些有趣的方法可以解决这个问题。正如其他人指出的那样,直接翻译成Kotlin应该是这样的:
val x = if (condition) result1 else result2
但就我个人而言,我认为这可能会有点混乱,很难阅读。库中还内置了一些其他选项。你可以将takeIf{}与elvis操作符一起使用:
val x = result1.takeIf { condition } ?: result2
这里发生的事情是takeIf{}命令返回result1或null,而elvis操作符处理null选项。还有一些额外的选项,例如takeUnless {}:
val x = result1.takeUnless { condition } ?: result2
语言很清楚,你知道它在做什么。
如果这是一个常用的条件,您还可以做一些有趣的事情,比如使用内联扩展方法。让我们假设我们想要追踪一个Int类型的游戏分数,并且我们想要在给定条件不满足时总是返回0:
inline fun Int.zeroIfFalse(func: () -> Boolean) : Int = if (!func.invoke()) 0 else this
好吧,这看起来很难看。但是考虑一下它在使用时的样子:
var score = 0
val twoPointer = 2
val threePointer = 3
score += twoPointer.zeroIfFalse { scoreCondition }
score += threePointer.zeroIfFalse { scoreCondition }
如您所见,Kotlin在选择如何表达代码方面提供了很大的灵活性。我的例子有无数的变化,可能我还没有发现的方法。我希望这能有所帮助!
使用以下中缀函数,我可以覆盖许多常见的用例,几乎与在Python中实现的方式相同:
class TestKotlinTernaryConditionalOperator {
@Test
fun testAndOrInfixFunctions() {
Assertions.assertThat(true and "yes" or "no").isEqualTo("yes")
Assertions.assertThat(false and "yes" or "no").isEqualTo("no")
Assertions.assertThat("A" and "yes" or "no").isEqualTo("yes")
Assertions.assertThat("" and "yes" or "no").isEqualTo("no")
Assertions.assertThat(1 and "yes" or "no").isEqualTo("yes")
Assertions.assertThat(0 and "yes" or "no").isEqualTo("no")
Assertions.assertThat(Date() and "yes" or "no").isEqualTo("yes")
@Suppress("CAST_NEVER_SUCCEEDS")
Assertions.assertThat(null as Date? and "yes" or "no").isEqualTo("no")
}
}
infix fun <E> Boolean?.and(other: E?): E? = if (this == true) other else null
infix fun <E> CharSequence?.and(other: E?): E? = if (!(this ?: "").isEmpty()) other else null
infix fun <E> Number?.and(other: E?): E? = if (this?.toInt() ?: 0 != 0) other else null
infix fun <E> Any?.and(other: E?): E? = if (this != null) other else null
infix fun <E> E?.or(other: E?): E? = this ?: other