Standard.kt一览

TODO

@kotlin.internal.InlineOnly
public inline fun TODO(): Nothing = throw NotImplementedError()

/**
 * Always throws [NotImplementedError] stating that operation is not implemented.
 *
 * @param reason a string explaining why the implementation is missing.
 */
@kotlin.internal.InlineOnly
public inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")

与java的//TODO很像,最大的区别是这个TODO会抛出异常
例:

fun main(args: Array<String>) {
    TODO()
}

Exception in thread “main” kotlin.NotImplementedError: An operation is not implemented.

或者:

fun main(args: Array<String>) {
    TODO("qfxl")
}

Exception in thread “main” kotlin.NotImplementedError: An operation is not implemented: qfxl

run

/**
 * Calls the specified function [block] and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <R> run(block: () -> R): R = block()

调用block并且返回block的结果

例:

 val qfxl = run {
        println("qfxl")
        "qfxl"
    }
    println(qfxl)

或者:

  val qfxl = "qfxl".run {
        println(this)
        this.substring(0,2)//this可以省略
    }
    println(qfxl)

输出

qfxl
qf

在run中,用this代表当前引用对象,并且调用其方法时,this可省略。
返回值是语句块的最后一行,若最后一行语句无返回值,则整个run语句块也无返回值

with

/**
 * Calls the specified function [block] with the given [receiver] as its receiver and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> with(receiver: T, block: T.() -> R): R = receiver.block()

执行接收类型为Tblock并且返回block的结果

例:

  val qfxl = with("qfxl") {
        println(this)
        substring(0, 2)
    }
    println(qfxl)

输出

qfxl
qf

also

@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }

执行参数为当前对象的block并且返回当前对象

例:

   val qfxl = "qfxl".also {
        println(it)
        it.substring(0, 2)
    }
    println(qfxl)//返回值依然是"qfxl"

输出

qfxl
qfxl

let

/**
 * Calls the specified function [block] with `this` value as its argument and returns its result.
 */
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R = block(this)

执行参数为当前对象的block并且block的结果

例:

  val qfxl = "qfxl".let {
        println(it)
        it.substring(0, 2)
    }
    println(qfxl)

输出

qfxl
qf

takeIf

/**
 * Returns `this` value if it satisfies the given [predicate] or `null`, if it doesn't.
 */
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeIf(predicate: (T) -> Boolean): T? = if (predicate(this)) this else null

条件成立返回该对象,否则返回null

例:

  val qfxl = "qfxl".takeIf {
        it.length > 5
    }
    println(qfxl)

输出

null

或者:

  val qfxl = "qfxl".takeIf {
        it.length > 1
    }
    println(qfxl)

输出

qfxl

takeUnless

/**
 * Returns `this` value if it _does not_ satisfy the given [predicate] or `null`, if it does.
 */
@kotlin.internal.InlineOnly
@SinceKotlin("1.1")
public inline fun <T> T.takeUnless(predicate: (T) -> Boolean): T? = if (!predicate(this)) this else null

*与takeIf相反,条件成立返回null,否则返回该对象*
例:

 val qfxl = "qfxl".takeUnless {
        it.length > 1
    }
    println(qfxl)

输出

null

或者:

  val qfxl = "qfxl".takeUnless {
        it.length > 5
    }
    println(qfxl)

输出

qfxl

repeat

/**
 * Executes the given function [action] specified number of [times].
 *
 * A zero-based index of current iteration is passed as a parameter to [action].
 */
@kotlin.internal.InlineOnly
public inline fun repeat(times: Int, action: (Int) -> Unit) {
    for (index in 0..times - 1) {
        action(index)
    }
}

源码可知

   repeat(3, {
        println("qfxl")
    })

输出

qfxl
qfxl
qfxl

总结

返回当前对象的操作符

apply、also

返回block结果的操作符

run、with、let

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值