Kotlin之Elvis 操作符

时间:2022-04-24
本文章向大家介绍Kotlin之Elvis 操作符,主要内容包括Elvis 操作符的优先级、与 ?. 配合使用时的问题、?: 与流程控制语句的搭配使用、改良 Elvis、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

Elvis 操作符的优先级

首先要注意到,Elvis 操作符的优先级是比较低的,特别是比加减乘除的运算符低,混合运算时一定要记得加上括号。比如:

fun <T> Iterable<T>.contentHashCode(): Int {
    return fold(1) {
        hash, element ->
        hash * 31 + (element?.hashCode() ?: 0)
    }
}

这里如果不用括号将 element?.hashCode() ?: 0 括起来,编译器就会认为这句表达式是 (hash * 31 + element?.hashCode()) ?: 0,出现编译错误。

与 ?. 配合使用时的问题

Elvis 操作符与安全调用符 ?. 配合使用时,一定要考虑到安全调用符前后是否为空,否则就会带来流程控制混乱的问题。对于任何一个下列的表达式:

val v = a?.b ?: c

因为 ?. 的优先级比 ?: 高,首先计算 a?.b,按照安全调用符的规则,如果 a == null 则结果为 null,执行 c,但如果 a.b == null,也会执行 c。也就是说,它的执行逻辑是这样的:

var temp = if(a != null) a.b else null
val v = if(temp != null) temp else c

它等价于:

val v = if(a == null || a.b == null) c else a.b

实际使用时一定要注意 ?. 前后是否都可能为 null。

?: 与流程控制语句的搭配使用

我发了一个 Kotlin 写的前序遍历二叉树的 Gist,地址在这里:PreOrderTraversing.kt,整个项目:DataStructureLearning-Kotlin 非递归遍历二叉树的代码如下:

private fun preOrderLoop(
    root: Node? = this.root,
    list: MutableList<Int> = mutableListOf()
): List<Int> {
    val stack = ArrayDeque<Node>()
    stack.push(root ?: return list)

    while (stack.isNotEmpty()) {
        val nodeNow = stack.pop()!!
        list += nodeNow.value
        nodeNow.right?.let { stack.push(it) }
        nodeNow.left?.let { stack.push(it) }
    }
    return list
}

第二句很有意思。ArrayDeque 不能容纳 null,一旦插入 null 就会抛出 NPE,而我们的函数要求 root 为 null 时返回一个空的 List,所以这里 push() 的参数写成 root ?: return list,这句代码的逻辑如下:

if(root == null) return list
stack.push(root)

此外,Elvis 还可以配合 break 和 continue 来控制循环流程。

改良 Elvis

Elvis 操作符很方便,但只能连接表达式,我们可以写一个扩展函数来作为加强版的 Elvis 操作符。

inline infix fun <T : Any> T?.ifNull(block: (T?) -> T): T {
    if (this == null) {
        return block(this)
    }
    return this
}

使用方式:

val file = java.io.File("C:\FakeFile")
val parent = file.parent ifNull {
    // do something here
}