当前位置:网站首页>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()}")
}
}边栏推荐
- LeetCode简单题之字符串中最大的 3 位相同数字
- opencv学习笔记五——梯度计算/边缘检测
- 互动送书-《Oracle DBA工作笔记》签名版
- 复杂网络建模(一)
- [quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
- 【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
- Lattice coloring - matrix fast power optimized shape pressure DP
- 解析机器人科技发展观对社会研究论
- Empire CMS collection Empire template program general
- Openjudge noi 2.1 1752: chicken and rabbit in the same cage
猜你喜欢

jeeSite 表单页面的Excel 导入功能

快解析内网穿透助力外贸管理行业应对多种挑战

饥荒云服管理脚本

解析机器人科技发展观对社会研究论

Leetcode simple question: find the K beauty value of a number

LeetCode简单题之字符串中最大的 3 位相同数字

Basic use of CTF web shrink template injection nmap
![[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)](/img/e1/9a047ef13299b94b5314ee6865ba26.png)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)

藏书馆App基于Rainbond实现云原生DevOps的实践

Record a stroke skin bone error of the skirt
随机推荐
LeetCode简单题之判断一个数的数字计数是否等于数位的值
LeetCode简单题之字符串中最大的 3 位相同数字
LeetCode简单题之找到一个数字的 K 美丽值
JS复制图片到剪切板 读取剪切板
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
央视太暖心了,手把手教你写HR最喜欢的简历
Unityhub cracking & unity cracking
Recursive method to construct binary tree from preorder and inorder traversal sequence
发挥创客教育空间的广泛实用性
eBPF Cilium实战(2) - 底层网络可观测性
Complex network modeling (II)
Network learning (II) -- Introduction to socket
基于Pytorch 框架手动完成线性回归
Four items that should be included in the management system of integral mall
JS copy picture to clipboard read clipboard
王爽 《汇编语言》之寄存器
Content of string
Réplication de vulnérabilité - désrialisation fastjson
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Vulnerability recurrence easy_ tornado