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()
執行接收類型爲T
的block
並且返回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