Maybe someone here can shed more light on this? <h...
# library-development
s
Maybe someone here can shed more light on this? https://kotlinlang.slack.com/archives/C7L3JB43G/p1678126163957399
👀 1
b
With it, a
val
can be initialized in the lambda. +1
runCatching
should/could have used it (unless I'm missing something). Where do you see the unreachable code warning?
s
It's actually not possible to add it for
runCatching
, or it results in a false positive. I know the benefits of using it, but we're getting false-negatives all over after adding it to Arrow. More context in Arrow.
b
interesting. On my side I see the erroneous warning whether I add the
contract
or not
🤔 Scratch that, it seems to be flaky. Now I don't see it with or without the contract 😅
s
Okay, that's really strange. For me it's consistent only with the
EXACTLTY_ONCE
contract. It's also been reported by other people (assuming using different versions of IDEA).
b
but yeah in any case, looks like a bug 🙂
s
Yes but in any case, that still poses the question to me why the Kotlin Std uses it so little 🤔 I.e. with the current version of
kotlin.runCatching
without
EXACTLY_ONCE
you cannot initialise
val
.
Screenshot 2023-03-07 at 20.11.31.png
b
I agree, it's odd - maybe
runCatching
existed prior to the contracts being introduced and they were just never added - but I'm just conjecturing
f
Not only the false-negative on "Unreachable code", but there is a more serious issue:
Copy code
val x: String
runCatching {
    if (1+1==2) throw Exception()
    x = "42"
}
println(x)
If runCatching had EXACTLY_ONCE, this code would compile with no problem, but x would not be initialized when the println statement is reached. In general, if your function catches any throwable of the block, it is not safe to add EXACTLY_ONCE AFAIS.