正在阅读apply函数代码源码,发现
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
Run Code Online (Sandbox Code Playgroud)
并且合约有一个空的主体,实验性的
@ContractsDsl
@ExperimentalContracts
@InlineOnly
@SinceKotlin("1.3")
@Suppress("UNUSED_PARAMETER")
public inline fun contract(builder: ContractBuilder.() -> Unit) { }
Run Code Online (Sandbox Code Playgroud)
合同的真正目的是什么,是否会保留在下一个版本中?
有人可以向我解释为什么此代码块通常会打印这些行,我们在何时使用!is in!等多种可能性来控制数据流,这是我的代码:
fun isNumber(obj: Any) {
when (obj) {
!is Long, Int, Float, Double -> {
println("No it's not a number")
}
else -> {
println("Yes it's a number")
}
}
}
fun main(args: Array<String>) {
isNumber(19.10)
isNumber(19L)
isNumber(19)
isNumber(19.10F)
}
Run Code Online (Sandbox Code Playgroud)
结果 :
No it's not a number
Yes it's a number
No it's not a number
No it's not a number
Run Code Online (Sandbox Code Playgroud)