当前位置:网站首页>Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
2022-07-07 08:14:00 【yu-Knight】
1. let The return type of is changed according to the change of anonymous function (lambda The return type of changes ) 2. The anonymous function holds it == I == info In itself inline fun <I, O> I.let(lambda:(I) -> O) = lambda(this) 1. apply The return type of is always I( So you can chain call ), lambda The return type of does not change, that is Unit, And not with lambda Association return type 2. I.() The anonymous extension function holds this == I == info In itself inline fun <I> I.apply(lambda:I.() -> Unit):I{ lambda() return this }
inline fun <R> run(block: () -> R): R {
return block()
}
//inline fun <R> run(block: () -> R): R = block()
inline fun <T, R> T.run(block: T.() -> R): R {
return block()
}
//inline fun <T, R> T.run(block: T.() -> R): R = block()
inline fun <T, R> with(receiver: T, block: T.() -> R): R {
return receiver.block()
}
//inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()
inline fun <T> T.apply(block: T.() -> Unit): T {
block()
return this
}
inline fun <T> T.also(block: (T) -> Unit): T {
block(this)
return this
}
public inline fun <T, R> T.let(block: (T) -> R): R {
return block(this)
}
//inline fun <T, R> T.let(block: (T) -> R): R = block(this)
inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? {
return if (predicate(this)) this else null
}
//inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null
inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? {
return if (!predicate(this)) this else null
}
//inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null
//inline Higher order functions use inline pairs lambda Optimize to improve performance
//fun <INPUT> A generic type is declared in the function
//INPUT.mApply Let all types call mApply spread function
//INPUT.() -> Unit Let our anonymous extension function hold this
// stay lambda There is no need to return value , There will always be return INPUT In itself
private inline fun <INPUT> INPUT.mApply(lambda: INPUT.() -> Unit): INPUT {
lambda()//this It can be omitted
return this
}
/**
* public inline fun <T> T.apply(block: T.() -> Unit): T {
* block()
* return this
* }
*/
fun main() {
val r: File = File("D:\\a.txt")
.mApply {
// The input is this == File Object itself
this.setReadable(true)
setWritable(true)
println("1 ${readLines()}")
}.mApply {
// The input is this == File Object itself
setReadable(true)
setWritable(true)
println("2 ${readLines()}")
}
}
边栏推荐
- 海信电视开启开发者模式
- Zcmu--1396: queue problem (2)
- 贝叶斯定律
- Complex network modeling (III)
- CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
- 快解析内网穿透助力外贸管理行业应对多种挑战
- Network learning (II) -- Introduction to socket
- Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
- Notes on PHP penetration test topics
- Blob object introduction
猜你喜欢
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
在 Rainbond 中一键安装高可用 Nacos 集群
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
JS quick start (I)
利用 Helm 在各类 Kubernetes 中安装 Rainbond
【雅思口语】安娜口语学习记录 Part2
JS cross browser parsing XML application
Codeforce c.strange test and acwing
王爽 《汇编语言》之寄存器
Myabtis_Plus
随机推荐
eBPF Cilium实战(1) - 基于团队的网络隔离
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
雅思考试自己的复习进度以及方法使用【日更版】
Application of slip ring of shipborne radar antenna
电池、电机技术受到很大关注,反而电控技术却很少被提及?
Recursive method constructs binary tree from middle order and post order traversal sequence
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
offer收割机:两个长字符串数字相加求和(经典面试算法题)
复杂网络建模(二)
Zsh shell adds automatic completion and syntax highlighting
Jmeter 的使用
海信电视开启开发者模式
快解析内网穿透助力外贸管理行业应对多种挑战
opencv学习笔记五——梯度计算/边缘检测
DNS server configuration
Pytoch (VI) -- model tuning tricks
LeetCode简单题之字符串中最大的 3 位相同数字
太真实了,原来自己一直没有富裕起来是有原因的
Bayes' law
在Rainbond中实现数据库结构自动化升级