ritesh
07/15/2024, 7:50 AM'when' expression must be exhaustive. Add an 'else' branch.
• I don't see any warning or lint error in AS IDE
• I can confirm, the when statement is exhuastivedmitriy.novozhilov
07/15/2024, 7:51 AMritesh
07/15/2024, 7:57 AMwhen (either) {
is Either.Success -> {
}
is Either.Failure -> {
}
}
sealed class Either<out S, out F> {
data class Success<S, F> internal constructor(val success: S) : Either<S, F>()
data class Failure<S, F> internal constructor(val failure: F) : Either<S, F>()
}
dmitriy.novozhilov
07/15/2024, 7:59 AMeither
?
Is it Either<*, *>
?ritesh
07/15/2024, 8:02 AMSingle<Either<Object1, Object2>>
gildor
07/15/2024, 8:02 AMdmitriy.novozhilov
07/15/2024, 8:04 AMsealed class Either<out S, out F> {
data class Success<S, F> internal constructor(val success: S) : Either<S, F>()
data class Failure<S, F> internal constructor(val failure: F) : Either<S, F>()
}
fun test(either: Either<Int, String>) {
val x = when (either) {
is Either.Success -> 1
is Either.Failure -> 2
}
}
This code compiles completely fine with 2.0.0dmitriy.novozhilov
07/15/2024, 8:05 AMSuccess
and Failure
is redundant
Such hierarchy will be much more useful
sealed class Either<out S, out F> {
data class Success<S>(val success: S) : Either<S, Nothing>()
data class Failure<F>(val failure: F) : Either<Nothing, F>()
}
dmitriy.novozhilov
07/15/2024, 8:06 AMeither
is Single<Either<Object1, Object2>>
, not Either<Object1, Object2>
?
And Single
is not part of Either
hierarchy?
In this case the compiler is entirely correctritesh
07/15/2024, 8:06 AMAre you sure that either is not nullable?I can cross check - 👀 In either case, the IDE should be warning for exhaustive when statement pre kotlin 2.0.0 as well right? I don't see any AS IDE error.
gildor
07/15/2024, 8:07 AMritesh
07/15/2024, 8:11 AMSingle<Either<Object1, Object2>>.subscribe { either ->
when(either) {
}
}
ritesh
07/15/2024, 8:33 AMval single:Single<Either<Object1,Object2>> = if (url == null) {
api.getSingleObject()
} else {
Single.just(Either.success(Object1()))
}
single.subscribe { either ->
when(either) {
Either.Success -> {}
Either.Failiure -> {}
}
}
• I can confirm it's not null, as the single
declaration is non-nullable.
If i don't use if statements and directly use one of the calls from if block or else block it compiles well.dmitriy.novozhilov
07/15/2024, 8:36 AMapi.get
?ritesh
07/15/2024, 8:39 AMdmitriy.novozhilov
07/15/2024, 8:41 AMritesh
07/15/2024, 8:42 AMSingle
is from Rx-java a third party library, a java classdmitriy.novozhilov
07/15/2024, 8:44 AMimport rx.Single
sealed class Either<out S, out F> {
data class Success<S, F> internal constructor(val success: S) : Either<S, F>()
data class Failure<S, F> internal constructor(val failure: F) : Either<S, F>()
companion object {
fun <S, F> success(x: S): Success<S, F> = Success(x)
}
}
class Object1
class Object2
class Api {
fun getSingleObject(): Single<Either<Object1, Object2>> = null!!
}
fun test(api: Api, cond: Boolean) {
val single: Single<Either<Object1, Object2>> = if (cond) {
api.getSingleObject()
} else {
Single.just(Either.success(Object1()))
}
single.subscribe { either ->
val x = when(either) {
is Either.Success -> 1
is Either.Failure -> 2
}
}
}
ritesh
07/15/2024, 11:25 AMapi.getSingleObject().subscribe(...)
or
Single.just(Either.success(Object1())).subscribe(..)
gets through the compiler, but not when using single
- 😕
I understand, the code you shared mimicks the same behaviour but.. 👀dmitriy.novozhilov
07/15/2024, 11:26 AMritesh
07/15/2024, 11:29 AMritesh
07/15/2024, 11:32 AMdmitriy.novozhilov
07/15/2024, 11:33 AMritesh
07/15/2024, 3:23 PMsingle.subscribe { either: Either<Object1, Object2> ->
val x = when(either) {
is Either.Success -> 1
is Either.Failure -> 2
}
}
Adding explicit type to the either in lambda, worked for us.
Still believe, it's an issue - will open a ticket with reproducing or sample project. Thanks for the help.dmitriy.novozhilov
07/15/2024, 3:24 PMgildor
07/16/2024, 3:34 AMritesh
07/16/2024, 5:27 PMgildor
07/18/2024, 3:32 AM