I'm not sure where to ask this: Does anyone know i...
# announcements
c
I'm not sure where to ask this: Does anyone know if
contract
will be able to ensure that if you do
precondition(someThing != null) { "Something is required" }
that
someThing
be detected as not null afterwards? Is this in the works?
d
Yes!
assertTrue
currently allows this.
c
I don't want to include
assertTrue
in my non-test code. 😶
d
I'm not saying to use
assertTrue
. I'm saying
assertTrue
uses the contract you described.
c
I'm going to check if there may be an issue on the topic.
d
Here's what it looks like.
Copy code
/** Asserts that the expression is `true` with an optional [message]. */
fun assertTrue(actual: Boolean, message: String? = null) {
    contract { returns() implies actual }
    return asserter.assertTrue(message ?: "Expected value to be true.", actual)
}
1
c
👍 Great
I just discovered that the
precondition
I have been using is my own. 🤦 So I'm adding
contract
! and
-Xuse-experimental=kotlin.contracts.ExperimentalContracts
😀
d
You can also use
require
if you don't want to add the experimental flag.
c
Is works! Deleting
!!
I wanted to throw specific exceptions
d
Of course.
@Dominaezzz thanks for your help