说我有一个这样的类(class):
data class URLAndPath(
val baseUrl: URL,
val path: String?
) {
val url get(): URL? =
try { path?.let { URL(baseUrl, it) } }
catch(_: Exception) { null }
init { require(path == null || url != null) { "Invalid URL $baseUrl$path" } }
}
此类可确保if且仅当
path != null
时Kotlin contracts似乎是告诉编译器这种关系的方法。以上不变式是否可以用Kotlin合约建模?
我的最终结果是让类似以下的代码进行编译:
val x = URLAndPath(URL("http://example.org/"), "index.html")
if(x.path != null) {
// currently: Error: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type URL?
println(x.url.toURI())
}
最佳答案
在Kotlin 1.3中,这似乎是不可能的,因为契约(Contract)只能在顶级功能上,而不能在方法上。
例如
@ExperimentalContracts
data class NullableString(val s: String?) {
fun isNotNull(): Boolean {
contract {
returns(true) implies (this@path != null)
}
return path != null
}
}
无法编译:
Error:(16, 8) Contracts are allowed only for top-level functions
Error:(17, 39) Unresolved reference: @path
Error:(19, 15) Unresolved reference: path
关于kotlin - Kotlin契约(Contract):链接两个属性不能为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53738456/