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