当前位置:网站首页>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()}")
}
}
边栏推荐
- Vulnerability recurrence fastjson deserialization
- Complex network modeling (I)
- Réplication de vulnérabilité - désrialisation fastjson
- OpenVSCode云端IDE加入Rainbond一体化开发体系
- B. Value sequence thinking
- 青龙面板-今日头条
- eBPF Cilium实战(2) - 底层网络可观测性
- [quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
- 解析创新教育体系中的创客教育
- LeetCode简单题之判断一个数的数字计数是否等于数位的值
猜你喜欢
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
船载雷达天线滑环的使用
解析机器人科技发展观对社会研究论
Niu Mei's mathematical problem --- combinatorial number
追风赶月莫停留,平芜尽处是春山
buureservewp(2)
Basic use of CTF web shrink template injection nmap
云原生存储解决方案Rook-Ceph与Rainbond结合的实践
Leetcode medium question my schedule I
解读创客思维与数学课程的实际运用
随机推荐
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
Leetcode 187 Repeated DNA sequence (2022.07.06)
Interactive book delivery - signed version of Oracle DBA work notes
Content of string
It took "7" years to build the robot framework into a micro service
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
漏洞复现-easy_tornado
JS复制图片到剪切板 读取剪切板
Unityhub cracking & unity cracking
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
Fast parsing intranet penetration escorts the document encryption industry
Qinglong panel -- Huahua reading
积分商城管理系统中应包含的四大项
复杂网络建模(一)
Bugku CTF daily one question chessboard with only black chess
Linux Installation MySQL 8.0 configuration
Interview questions (CAS)
机器人教育在动手实践中的真理
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
Topic not received? Try this