What is the clean way to shortcut execution based ...
# arrow
c
What is the clean way to shortcut execution based on a boolean value in an
either
block? I currently have:
Copy code
if (some condition) {
  val left: Either<Failure, DbUser> =
    UnknownFailure("some message here")
      .left()
  left.bind()
}
I would have just written
Copy code
if (some condition)
    UnknownFailure("message").left().bind()
however, that casts the entire either block to
Either<Failure, Nothing>.
c
Make the validation another function that returns an either
c
It's just a validation though, there is no meaningful
Right
. Unless I make it have a
Right(Unit)
, but that doesn't seem that clean
c
I often use a Unit for my right path on validation
🔝 1
There is actually a built in helper when you specifically want to do that with
.void()
This is also a good use case for
Either.conditionally(condition, {err}, {Unit}).bind()
👍 3
Copy code
fun validateCondition(foo: Foo) = Either.conditionally(foo.isValid, {UnknownFailure("bar")}, {Unit})
k
@CLOVIS One step on the path to point free goodness is to not have imperative
if
statements. Passing functions to functions like
Either.conditionally
is the way to make the code cleaner.