当前位置:网站首页>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()}")
}
}边栏推荐
- Openjudge noi 2.1 1752: chicken and rabbit in the same cage
- JS quick start (I)
- 发挥创客教育空间的广泛实用性
- Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
- Leetcode medium question my schedule I
- Network learning (I) -- basic model learning
- 调用 pytorch API完成线性回归
- 快解析内网穿透为文档加密行业保驾护航
- ROS bridge notes (05) - Carla_ ackermann_ Control function package (convert Ackermann messages into carlaegovehiclecontrol messages)
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
猜你喜欢

Niu Mei's mathematical problem --- combinatorial number

Codeforce c.strange test and acwing

CTF-WEB shrine模板注入nmap的基本使用

Easy to understand SSO

UnityHub破解&Unity破解

Interactive book delivery - signed version of Oracle DBA work notes

漏洞複現-Fastjson 反序列化

uniapp 移动端强制更新功能
![[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO](/img/56/82f4533b5bded73df222ef65101a72.png)
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO

jeeSite 表单页面的Excel 导入功能
随机推荐
探索STEAM艺术设计中的创造力
青龙面板--整理能用脚本
一文了解如何源码编译Rainbond基础组件
LeetCode简单题之判断一个数的数字计数是否等于数位的值
面试题(CAS)
太真实了,原来自己一直没有富裕起来是有原因的
The legend about reading the configuration file under SRC
Bayes' law
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
漏洞复现-easy_tornado
Zsh shell adds automatic completion and syntax highlighting
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
Zcmu--1396: queue problem (2)
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
Pytoch (VI) -- model tuning tricks
Uniapp mobile terminal forced update function
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
Empire CMS collection Empire template program general
Complex network modeling (II)
Offer harvester: add and sum two long string numbers (classic interview algorithm question)