https://kotlinlang.org logo
#announcements
Title
# announcements
c

corneil

08/28/2019, 7:05 PM
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

Dominaezzz

08/28/2019, 7:28 PM
Yes!
assertTrue
currently allows this.
c

corneil

08/28/2019, 7:30 PM
I don't want to include
assertTrue
in my non-test code. 😶
d

Dominaezzz

08/28/2019, 7:31 PM
I'm not saying to use
assertTrue
. I'm saying
assertTrue
uses the contract you described.
c

corneil

08/28/2019, 7:31 PM
I'm going to check if there may be an issue on the topic.
d

Dominaezzz

08/28/2019, 7:32 PM
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

corneil

08/28/2019, 7:33 PM
👍 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

Dominaezzz

08/28/2019, 7:39 PM
You can also use
require
if you don't want to add the experimental flag.
c

corneil

08/28/2019, 7:40 PM
Is works! Deleting
!!
I wanted to throw specific exceptions
d

Dominaezzz

08/28/2019, 7:41 PM
Of course.
@Dominaezzz thanks for your help