Davide Giuseppe Farella
03/25/2021, 1:10 PMEither
, which is the best way to get left?
Example
// ok
assertEquals(expectedResult, either.orNull())
// how?
assertEquals(expectedError, either.<leftOrNull()>)
Javier
03/25/2021, 1:14 PMgetSomethingOrNull()
, should be interesting in having something like getLeft()
and getLeftOrNull()
albertosh
03/25/2021, 1:17 PMeither.fold(::identity) { throw RuntimeException("I don't want a right") }
Davide Giuseppe Farella
03/25/2021, 1:19 PMalbertosh
03/25/2021, 1:19 PMalbertosh
03/25/2021, 1:19 PMgetOrHandle()
fits your usecaseDavide Giuseppe Farella
03/25/2021, 1:20 PMsimon.vergauwen
03/25/2021, 1:24 PMleftOrNull()
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
.Jannis
03/25/2021, 1:26 PMassertEquals(Either.left(error), either)
since that won't produce weird assertions if you ever have either = Either.Left(null)
Davide Giuseppe Farella
03/25/2021, 1:28 PM*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 projectstojan
03/25/2021, 1:57 PMeither.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
Cody Mikol
03/25/2021, 2:36 PMfun <L, R> Either<L, R>.getLeftOrNull() = this.fold({ it }, { null })
Davide Giuseppe Farella
03/25/2021, 4:30 PMyou are usingBut 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.because you are not sure if it’s aEither
or aLeft
.... if you know it’s always theRight
value, then you don’t need theLeft
Either
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
Either
- Left
- OneErrorType
- AnotherErrorType
- Right
_ Whatever
if (either.isLeft()) {
when (either.getLeft())
OneErrorType -> doSomething()
AnotherErrorType -> doAnotherThing()
}
}
simon.vergauwen
03/25/2021, 4:37 PMif (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.
when(either) {
is Left -> when(either.value) {
OneErrorType -> doSomething()
AnotherErrorType -> doAnotherThing()
}
is Right -> noErrorFunction()
}
Davide Giuseppe Farella
03/25/2021, 8:09 PMWritingIt is, I wrote it incomplete just as an example, but what you said is totally true! Thanks 🙂withoutif
is considered a bad patternelse
Chris Paul
03/29/2021, 8:55 AMassertEquals(expectedError.left(), either)