kotlin - kotlin 中的 use{} 和 apply{} 有什么区别?
问题描述
cursor?.apply{
this.moveToFirst()
}
cursor?.use{
it.moveToFirst()
}
我看到的唯一区别是it
and this
,这是同一个实例。但是,还有其他区别吗?
解决方案
use()
只能用Closeable
接收器调用。块执行后,Closeable
资源关闭,节省样板文件。
您可以查看https://kotlinlang.org/docs/scope-functions.html以获取有关所有范围函数的更多信息。use()
本质上与 相同let()
,除了具有额外的自动关闭功能。
推荐阅读
- c# - .net core 3.0 AOT JIT 第一次调用还是很慢
- java - 注释作为对象的替代品?
- pandas - 如何使用熊猫将基于时间的列保存到 excel 文件中?
- python - 如何将 Python 对象(不是 JSON 可序列化的)从一个(AWS)lambda 函数传递到另一个?
- excel - 更新库存清单
- c - 在 C 中使用大小为 10 的一维数组添加偶数
- javascript - 解析错误:意外的令牌,React 中的预期“}”
- html - 图像未显示 data-bg-src
- docker - 如何在 Nexus3 中为 GitLab 容器注册表配置 docker 代理存储库?
- sql - 内部连接多个表,但想要基于一列的不同数据