Kotlin 合约:两个属性的链接非空

dte*_*ech 4 kotlin kotlin-contracts

假设我有一堂这样的课:

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" } } 
}
Run Code Online (Sandbox Code Playgroud)

这个类确保当path != null且仅当url != null

Kotlin 契约似乎是告诉编译器这些关系的方式。上述不变量可以用 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())
}
Run Code Online (Sandbox Code Playgroud)

dte*_*ech 5

这在 Kotlin 1.3 中似乎不可能,因为合约只能在顶级函数上,所以不能在方法上。

例如

@ExperimentalContracts
data class NullableString(val s: String?) {

    fun isNotNull(): Boolean {
        contract {
            returns(true) implies (this@path != null)
        }
        return path != null
    }
}
Run Code Online (Sandbox Code Playgroud)

不编译:

Error:(16, 8) Contracts are allowed only for top-level functions
Error:(17, 39) Unresolved reference: @path
Error:(19, 15) Unresolved reference: path
Run Code Online (Sandbox Code Playgroud)