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(,)//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()
執行接收類型為
T
的
block
并且傳回
block
的結果
例:
val qfxl = with("qfxl") {
println(this)
substring(, )
}
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(, )
}
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(, )
}
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 >
}
println(qfxl)
輸出
null
或者:
val qfxl = "qfxl".takeIf {
it.length >
}
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 >
}
println(qfxl)
輸出
null
或者:
val qfxl = "qfxl".takeUnless {
it.length >
}
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 .times - ) {
action(index)
}
}
源碼可知
repeat(, {
println("qfxl")
})
輸出
qfxl
qfxl
qfxl
總結
傳回目前對象的操作符
apply、also
傳回block結果的操作符
run、with、let