Kotlin Standart函数

来源:互联网 发布:sql 时间戳转换成时间 编辑:程序博客网 时间:2024/05/21 16:35

    • TODO
    • run
    • with
    • apply
    • also
    • let
    • takeIf
    • takeUnless
    • repeat

Standard.kt文件下

TODO()

/** * An exception is thrown to indicate that a method body remains to be implemented. */public class NotImplementedError(message: String = "An operation is not implemented.") : Error(message)/** * Always throws [NotImplementedError] stating that operation is not implemented. */@kotlin.internal.InlineOnlypublic 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.InlineOnlypublic inline fun TODO(reason: String): Nothing = throw NotImplementedError("An operation is not implemented: $reason")

run()

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

with()

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

apply()

/** * Calls the specified function [block] with `this` value as its receiver and returns `this` value. */@kotlin.internal.InlineOnlypublic inline fun <T> T.apply(block: T.() -> Unit): T { block(); return this }

also()

/** * Calls the specified function [block] with `this` value as its argument and returns `this` value. */@kotlin.internal.InlineOnly@SinceKotlin("1.1")public inline fun <T> T.also(block: (T) -> Unit): T { block(this); return this }

let()

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

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

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

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.InlineOnlypublic inline fun repeat(times: Int, action: (Int) -> Unit) {    for (index in 0..times - 1) {        action(index)    }}
原创粉丝点击