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

Sourabh Rawat

12/16/2021, 7:09 PM
Hi, I have an
either
block from which I want to return a
Left
value without any
bind
calls. For example
Copy code
either<Error, Response> {
 when(foo) {
  1 -> callFunReturningEither().bind()
  else -> Error("foo is in else") <-- How to return this Left value?
 }
}
I have tried
Error("foo is in else").left().bind()
but it gives
Returning type parameter has been inferred to Nothing implicitly. Please specify type arguments explicitly to hide this warning. Nothing can produce an exception at runtime.
which I think is expected.
r

raulraja

12/16/2021, 7:35 PM
Hi @Sourabh Rawat, not sure if there is additional code in the mix but if you are not using
bind
there is no point of wrapping in an either block. It would be in this case just:
Copy code
val result: Either<Error, Response> = 
  when(foo) {
    1 -> callFunReturningEither()
    else -> Either.Left(Error("foo is in else"))
   }
s

simon.vergauwen

12/16/2021, 8:20 PM
Copy code
when(foo) {
  1 -> callFunReturningEither()
  else -> Error("foo is in else").left()
 }
That is not an error the compiler gave you there though, and the code will work as you expect when doing.
Copy code
either<Error, Response> {
 when(foo) {
  1 -> callFunReturningEither()
  else -> Error("foo is in else").left().bind()
 }
}
p

pakoito

12/16/2021, 8:53 PM
you have to
bind
the return of
1 ->
I believe
s

Sourabh Rawat

12/17/2021, 4:31 AM
Oh yea sorry, there was a typo in the original code. It does have
bind
call in
1 ->
clause
👍🏼 1
7 Views