Yeah, it won’t work because Either does not have a filter(actually withFilter) method.
And this is because there is no meaningful way to filter any Either, but you can easily provide some error value wrapped on a Left when you filter that value.
So you can do something like this:
_ <- if (a.nonEmpty) doSomethingWithAList(a) else Left("error")
(replace "error" with an appropriated value)
That way the computation will be halted.
However, a better design is to avoid impossible states altogether, something like this using cats.
import cats.data.NonEmptyList
type Error = String
type ErrorOr[+A] = Either[Error, A]
defgetSomeList(): ErrorOr[List[Foo]]
def doSomethingWithAList(list: NonEmptyList[Foo]): ErrorOr[Unit]
for {
a <- getSomeList()
aNonEmpty <- NonEmptyList.fromList(a).toRight(left = "a was empty")
_ <- doSomethingWithAList(aNonEmpty)
} yield a
Thank you for your reply! I still try to find the time to look into cats. It’s mentioned everywhere and so I believe I could benefit from it, too. But for now I have to stick to plane Scala.
I suppose this works the same way if I want to return a Right what so ever? Both, empty and non-empty, are valid states, I just cannot modify the doSomethingWithAList and therefore I cannot put an if there.
Not sure what you mean.
So NonEmptyList.fromList returns an Option, and I am transforming that Option into an Either by providing my own left value.
That is the same as
_ <- if (a.nonEmpty) doSomethingWithAList(a) else Left("error")
I recommend using a Left because it doesn’t make much sense to pass an erroneous value into the Right.