当前位置:网站首页>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()}")
        }
}

原网站

版权声明
本文为[yu-Knight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130641028421.html