使用 Kotlin 中的 takeIf

简评:在 Kotlin 的 标准函数中有个  takeIf 函数,你是否了解这个函数呢?其是否只是漂亮点的  if 语句呢?

先来看一个 takeIf 的例子,理论上你可以把代码中的所有 if 替换成 takeIf(虽然不推荐)。

// Original Code
if (status) { doThis() }

// Modified Code
takeIf { status }?.apply { doThis() }

和其他的所有函数一样,takeIf 也有自己合适的使用场景。为了更好的理解,先看看 takeIf 的具体实现:

public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? 
    = if (predicate(this)) this else null

从这里我们可以注意到:

  1. 是从 T 对象本身调用,即 T.takeIf;
  2. predicate 函数将 T 对象作为参数;
  3. 根据 predicate 的结果最终返回 this 或 null。

根据以上的特点,我们可以推导出其合适的用法:

1. 因为其由 T 对象本身调用,因此可以更方便的处理 null 值问题:

// Original code
if (someObject != null && status) {
   doThis()
}

// Improved code
someObject?.takeIf{ status }?.apply{ doThis() }

2. 因为 predicate 函数以 T 对象本身作为参数,因此也可以很方便的判断对象自身的某个属性是否为 null:

// Original code
if (someObject != null && someObject.status) {
   doThis()
}
// Better code
if (someObject?.status == true) {
   doThis()
}
// Improved code
someObject?.takeIf{ it.status }?.apply{ doThis() }

3. 由于当条件为 true 时,最终结果返回的是 this,因此可以进行链式操作:

val index 
   = input.indexOf(keyword).takeIf { it >= 0 } ?: error("Error")
val outFile 
   = File(outputDir.path).takeIf { it.exists() } ?: return false

另外,对于 takeIf 的使用有一个需要小心的地方,先看看下面的代码:

// Syntactically still correct. But logically wrong!!
someObject?.takeIf{ status }.apply{ doThis() }

// The correct one (notice the nullability check ?)
someObject?.takeIf{ status }?.apply{ doThis() }

第一行中的 doThis() 方法一定会执行而无论 status 是 true 还是 false,因为 doThis() 不是 someObject 中的方法。

这里 takeIf 后的 ? 符是很容易忽视但又非常重要的。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值