https://kotlinlang.org logo
#arrow
Title
# arrow
s

Sam

08/22/2022, 3:25 PM
This has probably been asked before, but I couldn’t find it from a quick search. Why doesn’t
shift
return
Nothing
? It’s declared as
Copy code
suspend fun <B> shift(r: R): B
but it could instead be
Copy code
suspend fun shift(r: R): Nothing
The reason I ask is specifically because of this use case:
Copy code
var failure: MyFailure? = null
val effect = eagerEffect<MyFailure, String> {
    failure?.let { shift(it) } // "Not enough information to infer type variable B"
    "some result"
}
The fix is to be explicit about the
Nothing
type, which feels super weird:
Copy code
val effect = eagerEffect<MyFailure, String> {
    failure?.let { shift<Nothing>(it) }
    "some result"
}
r

raulraja

08/22/2022, 4:41 PM
I think we had encoded it at some point with
Nothing
but due to how inference worked it was causing issues. It may be possible now. Technically
Nothing
is more correct than any type because after
shift
is called all statements and code afterward is unreachable.
Maybe worth a proof of concept PR to see if tests and everything else passes when shift return is changed to Nothing. If it works maybe we can change it in 2.0
s

simon.vergauwen

08/23/2022, 6:42 AM
This follows the same pattern as
emptyList
from Kotlin Std. Requiring
A
rather than
Nothing
. IIRC, this is the recommended pattern for inference in Kotlin but I might be mistaken.
s

Sam

08/23/2022, 7:39 AM
That’s a good comparison, I hadn’t considered it 👍. I would also compare it to
error(...)
which isn’t generic and always returns
Nothing
. I can see why
emptyList
uses the generic, but my sense is that
shift
is more similar to
error
than to
emptyList
.
s

simon.vergauwen

08/23/2022, 7:40 AM
but my sense is that
shift
is more similar to
error
than to
emptyList
Yes, very valid. We hadn't made this comparison before 🤔 Good candidate for Arrow 2.0 improvement as @raulraja said. Feel free to make a ticket for this on the Github repo so we can keep track of it 👍
s

simon.vergauwen

08/23/2022, 8:25 AM
Thank you for the very complete issue report @Sam 🙏
s

Sam

10/10/2022, 10:21 AM
8 Views