Kotlin三元条件算子

本文翻译自:Kotlin Ternary Conditional Operator

What is the equivalent of this expression in Kotlin? Kotlin中这个表达的等价物是什么?

a ? b : c

This is not valid code in Kotlin. 这不是Kotlin中的有效代码。


#1楼

参考:https://stackoom.com/question/16Xrw/Kotlin三元条件算子


#2楼

In Kotlin, if statements are expressions. 在Kotlin中, if语句是表达式。 So the following code is equivalent: 所以下面的代码是等价的:

if (a) b else c

The distinction between expression and statement is important here. 表达和陈述之间的区别在这里很重要。 In Java/C#/JavaScript, if forms a statement, meaning that it does not resolve to a value. 在Java / C#/ JavaScript中, if形成一个语句,意味着它不会解析为某个值。 More concretely, you can't assign it to a variable. 更具体地说,您无法将其分配给变量。

// Valid Kotlin, but invalid Java/C#/JavaScript
var v = if (a) b else c

If you're coming from a language where if is a statement, this might seem unnatural but that feeling should soon subside. 如果你来自一种语言if is a statement,这可能看起来不自然,但这种感觉很快就会消退。


#3楼

For myself I use following extension functions: 对于我自己,我使用以下扩展功能:

fun T?.or<T>(default: T): T = if (this == null) default else this 
fun T?.or<T>(compute: () -> T): T = if (this == null) compute() else this

First one will return provided default value in case object equals null. 如果对象等于null,则第一个将返回提供的默认值。 Second will evaluate expression provided in lambda in the same case. 其次将评估在同一情况下在lambda中提供的表达式。

Usage: 用法:

1) e?.getMessage().or("unknown")
2) obj?.lastMessage?.timestamp.or { Date() }

Personally for me code above more readable than if construction inlining 就个人而言,我上面的代码超过可读if建设内联


#4楼

Take a look at the docs : 看看文档

In Kotlin, if is an expression, ie it returns a value. 在Kotlin中,if是一个表达式,即它返回一个值。 Therefore there is no ternary operator (condition ? then : else), because ordinary if works fine in this role. 因此没有三元运算符(条件?则:else),因为普通的if在这个角色中工作正常。


#5楼

You could define your own Boolean extension function that returns null when the Boolean is false to provide a structure similar to the ternary operator: 您可以定义自己的Boolean扩展函数,当Booleanfalse时返回null ,以提供类似于三元运算符的结构:

infix fun <T> Boolean.then(param: T): T? = if (this) param else null

This would make an a ? b : c 这会成为a ? b : c a ? b : c expression translate to a then b ?: c , like so: a ? b : c表达式转换为a then b ?: c ,如下所示:

println(condition then "yes" ?: "no")

Update: But to do some more Java-like conditional switch you will need something like that 更新:但是要做一些类似Java的条件切换,你需要这样的东西

infix fun <T> Boolean.then(param: () -> T): T? = if (this) param() else null

println(condition then { "yes" } ?: "no") pay attention on the lambda. println(condition then { "yes" } ?: "no")注意lambda。 its content calculation should be postponed until we make sure condition is true 其内容计算应推迟到我们确保conditiontrue

This one looks clumsy, that is why there is high demanded request exist to port Java ternary operator into Kotlin 这个看起来很笨拙, 这就是为什么存在将Java三元运算符移植到Kotlin中的高要求


#6楼

when replaces the switch operator of C-like languages. 当替换类C语言的switch操作符时。 In the simplest form it looks like this 在最简单的形式,它看起来像这样

when (x) {
    1 -> print("x == 1")
    2 -> print("x == 2")
    else -> {
        print("x is neither 1 nor 2")
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值