Jose A.
05/18/2021, 4:52 PM@OptIn(ExperimentalContracts::class)
private fun OriginQueryNode.isSimpleQuery(): Boolean {
contract {
returns(true) implies (this@isSimpleQuery is OriginQueryNode.FilterAttributes)
}
return this is OriginQueryNode.FilterAttributes && ...
}
private fun calculateMembersLocallyIfPossible(originQuery: OriginQueryNode): MemberGaussCollection? =
if (originQuery.isSimpleQuery()) {
println(originQuery.fieldOnlyFilterAttributes)
}
In the `calculateMembersLocallyIfPossible`fun I try to access the field `fieldOnlyFilterAttributes`after checking isSimpleQuery
is true. The contract should let me access the field but it's not working (marks fieldOnlyFilterAttributes
as not existent.Jose A.
05/18/2021, 4:56 PMif (originQuery.isSimpleQuery() && originQuery is OriginQueryNode.FilterAttributes) {
then it works.Jose A.
05/18/2021, 4:58 PMnanodeath
05/18/2021, 5:01 PMJose A.
05/18/2021, 5:04 PMimport kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
sealed class A(val a: String) {
class B(val b: String): A("a")
}
@OptIn(ExperimentalContracts::class)
fun A.isNice(): Boolean {
contract {
returns(true) implies (this@isNice is A.B)
}
return this is A.B && b == "nice"
}
fun A.printIfNice() {
if (isNice())
println("It's nice! ${this.b}")
}
fun main() {
val someB = A.B("nice")
someB.printIfNice()
}
And this works as expectednanodeath
05/18/2021, 5:09 PMJose A.
05/18/2021, 5:12 PMJose A.
05/18/2021, 5:12 PMsealed class OriginQueryNode {
class FilterAttributes(
val attributeName: String,
val operator: ComparisonOperator,
val comparables: List<String>,
val comparablesAsExpression: List<String>
) : OriginQueryNode()
class SetOperation(...) : OriginQueryNode()
}
Jose A.
05/18/2021, 5:15 PM@OptIn(ExperimentalContracts::class)
private fun OriginQueryNode.isSimpleQuery(): Boolean {
contract {
returns(true) implies (this@isSimpleQuery is OriginQueryNode.FilterAttributes)
}
return this is OriginQueryNode.FilterAttributes && ...
}
Jose A.
05/18/2021, 5:18 PMnanodeath
05/18/2021, 5:24 PMJose A.
05/18/2021, 5:29 PMnanodeath
05/18/2021, 5:33 PMthis
is being checked in the contract blocknanodeath
05/18/2021, 5:33 PMthis@isSimpleQuery
but then the code right after just says this
Jose A.
05/18/2021, 5:39 PMimport kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
sealed class A(val a: String) {
class B(val b: String) : A("a")
}
class OtherClass {
fun someTest(a: A) {
a.printIfNice()
}
@OptIn(ExperimentalContracts::class)
private fun A.isNice(): Boolean {
contract {
returns(true) implies (this@isNice is A.B)
}
return this is A.B && b == "nice"
}
private fun A.printIfNice() {
if (isNice()) {
println(this.b)
}
}
}
fun main() {
val someB = A.B("nice")
val c = OtherClass()
c.someTest(someB)
}
Jose A.
05/18/2021, 5:44 PMnanodeath
05/18/2021, 5:49 PM