I have built the following implementation of unless:
inline fun unless(cond: Boolean, fn: () -> Unit) {
contract { callsInPlace(fn, InvocationKind.AT_MOST_ONCE) }
if (!cond) {
fn()
}
}
Is there a way to specify in the contract that fn will only be invoked if cond is false? That is needed in order for smart casts to work in the following code:
unless(x is Foo) {
throw SomeException("x is not Foo");
}
x.someMethodOnFoo() // This will not smart cast
Yes. That is clear to me. However, since the function is inlined, I would have expected the Kotlin compiler to be able to figure this out. Arguably it should be able to find that out even without any contracts.