当前位置:网站首页>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()}")
}
}
边栏推荐
- 【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
- Interview questions (CAS)
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- 复杂网络建模(二)
- 海信电视开启开发者模式
- What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
- Codeforce c.strange test and acwing
- Bugku CTF daily one question chessboard with only black chess
- Recursive method constructs binary tree from middle order and post order traversal sequence
- 积分商城管理系统中应包含的四大项
猜你喜欢
Codeforce c.strange test and acwing
[quick start of Digital IC Verification] 13. SystemVerilog interface and program learning
JS cross browser parsing XML application
【雅思口语】安娜口语学习记录 Part2
opencv学习笔记五——梯度计算/边缘检测
Leetcode medium question my schedule I
[step on the pit series] H5 cross domain problem of uniapp
Game attack and defense world reverse
The largest 3 same digits in the string of leetcode simple question
青龙面板-今日头条
随机推荐
Recursive method constructs binary tree from middle order and post order traversal sequence
Basic use of CTF web shrink template injection nmap
Topic not received? Try this
Avatary's livedriver trial experience
Use of JMeter
CDC (change data capture technology), a powerful tool for real-time database synchronization
Codeforce c.strange test and acwing
漏洞复现-Fastjson 反序列化
快解析内网穿透助力外贸管理行业应对多种挑战
ZCMU--1396: 队列问题(2)
Notes on PHP penetration test topics
太真实了,原来自己一直没有富裕起来是有原因的
eBPF Cilium实战(1) - 基于团队的网络隔离
OpenVSCode云端IDE加入Rainbond一体化开发体系
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
【雅思口语】安娜口语学习记录 Part3
ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
Blob 对象介绍
Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
Interactive book delivery - signed version of Oracle DBA work notes