Good morning, regarding `Either`, which is the bes...
# arrow
d
Good morning, regarding
Either
, which is the best way to get left? Example
Copy code
// ok
assertEquals(expectedResult, either.orNull())

// how?
assertEquals(expectedError, either.<leftOrNull()>)
j
Reading the documentation I am not finding how to do this. Known that Kotlin is using the convention `getSomething()`(if it is null, it will crash) and
getSomethingOrNull()
, should be interesting in having something like
getLeft()
and
getLeftOrNull()
a
either.fold(::identity) { throw RuntimeException("I don't want a right") }
d
@Javier there’s none @albertosh thanks, I was hoping in something out of the box; I’ll add some extensions for fit our needing
Looks like
getOrHandle()
fits your usecase
👀 1
d
Not really, it will still give me a Right 🙂
s
I think
leftOrNull()
would make sense on
Either
, we also have it for inclusive or,
Ior
, so I think it makes sense to also have it for
Either
.
j
For testing you could also use
assertEquals(Either.left(error), either)
since that won't produce weird assertions if you ever have
either = Either.Left(null)
d
@simon.vergauwen It would be great, as now I added to our project as an extension. Maybe also
*OrThrow
variants would make sense, as we have
Either.catch { iCanThrow() }
, so a way out of exception, but a way in ( not ideal, but most of the products won’t be rewritten from scratch, in order to add Arrow 🙂 ) @Jannis Thanks! That looks nice! But probably I’ll go for the extensions, in order to easily integrate in our project
s
https://kotest.io/docs/assertions/arrow.html if you are using Kotest, it has built in arrow assertions..... so you can do
either.shouldBeLeft(v)
otherwise I would go with what @Jannis recommended
which is the best way to get left?
to answer this first you need to answer what happens if it's
Right
, default value? throw? null? you are using
Either
because you are not sure if it's a
Left
or a
Right
.... if you know it's always the
Left
value, then you don't need the
Either
1
👍 2
c
This should work for your case I believe
Copy code
fun <L, R> Either<L, R>.getLeftOrNull() = this.fold({ it }, { null })
d
you are using 
Either
 because you are not sure if it’s a 
Left
  or a 
Right
 .... if you know it’s always the 
Left
  value, then you don’t need the 
Either
But since we have a function for know if it’s left, I don’t see why I can’t get the actual Left. I understand that the ideal behind it is to go functional, but: 1.
isLeft
doesn’t sound so functional 2. considering a pre-existent project, where you don’t wanna go functional everywhere ( yet ), the following scenario would be pretty common
Copy code
Either
  - Left
    - OneErrorType
    - AnotherErrorType
  - Right
    _ Whatever

if (either.isLeft()) {
  when (either.getLeft())
    OneErrorType -> doSomething()
    AnotherErrorType -> doAnotherThing()
  }
}
👍 1
s
Copy code
if (either.isLeft()) {
  when (either.getLeft())
    OneErrorType -> doSomething()
    AnotherErrorType -> doAnotherThing()
  }
}
Writing
if
without
else
is considered a bad pattern. In this case you would write the following, and here the Kotlin compiler will do smart casting for you. Using Kotlin Contracts we can perhaps also enable this for
isLeft
but that needs to be investigated.
Copy code
when(either) {
  is Left -> when(either.value) {
     OneErrorType -> doSomething()
     AnotherErrorType -> doAnotherThing()
  }
  is Right -> noErrorFunction()
}
d
Writing 
if
 without 
else
 is considered a bad pattern
It is, I wrote it incomplete just as an example, but what you said is totally true! Thanks 🙂
c
similar to previous answer you could also do this:
assertEquals(expectedError.left(), either)