How come Kotlin contracts are not working anymore? These have been working fine a few months back.
r
Richard Gomez
11/09/2021, 1:45 PM
Interesting. I don't think that's ever worked for me.
Because
channel
is a class property, it could have mutated between the
requireNotNull
and usage.
I usually need to do:
Copy code
val channel = requireNotNull(this.channel)
<http://channel.cd|channel.cd>(..)
//or
channel.?let { // use }
👍 1
j
jeggy
11/09/2021, 2:12 PM
I usually do that ?.let pattern also. But it doesn't always end in the cleanest code. I did use the contracts like shown on the image some months ago. So there is something that has changed.
But yeah, I will go ahead and do
val channel = requireNotNull(this.channel)
instead of having
!!
all the time 🙂
e
ephemient
11/09/2021, 7:20 PM
smart casting should work on non-captured locals,
Copy code
fun foo() {
var bar: Int?
bar = 1
println(bar.inc())
}
but you can't prove that class properties won't be mutated by anybody else