https://kotlinlang.org logo
Title
m

Mazhar Ibna Zahur

02/28/2023, 7:08 AM
can anyone tell me what is correct way to write Either<A,B> ? when i set Exception and when Error in Left Side ? As i want to set result in the Right.
t

Ties

02/28/2023, 8:03 AM
you mean something like this?:
fun foo(bar : Boolean) : Either<Exception, String> {
   return if(bar) {
      Either.left(new RuntimeException())
   } else {
      Either.right("correct")
   }
}
It is also possible to do it "automatically" for exceptions:
fun foo(bar : Boolean) : Either<Exception, String> {
   return Either.catch {
     if(bar) {
      throw new RuntimeException()
   } else {
      "correct"
   }
}
}
m

Mazhar Ibna Zahur

02/28/2023, 8:05 AM
Yes. Thank you