I posted a few questions about failover in Scala, and I really thank all of you for your answers.
I understand my options when dealing with Either and Scalaz, or for understanding, and I have another (last?) Question:
How to perform a fault-tolerant sequence of operations when operations deal with an external non-functional world, such as DB?
I mean, I have a method like this:
def insertItem(item: Item): Either[Error,Item]
Thanks to Either and these answers, I know how to do this with: calls to the chaining method using either , Checking the method parameters in Scala, for understanding and monads
But the case Item class is immutable, and there is no point in returning it as Right , since the caller already has a value.
So how can I do the same with:
def insertItem(item: Item): Option[Error]
In my application, when the user is created, we also create some elements for him. If the item cannot be created, then the whole process should stop.
When I use Option[Error] directly for understanding, I don’t think I will get the expected result.
I think it makes sense to do something like this:
for { _ <- insertItem(item1).toLeft("???").right _ <- insertItem(item2).toLeft("???").right _ <- insertItem(item3).toLeft("???").right }
But since the meanings are "???" I put in my rights, it’s never useful, I think that I don’t have an elegant solution that is not related to creating rights that will never be used.
I think I'm looking for something that will continue to be understood only when the result is None , which looks strange, because I just want to continue the next operation and not make a real map operation.
By the way, if possible, I would like both non-Scalaz solutions and Scalaz solutions. I'm not sure if Scalaz handles such things because it seems more focused on real functional programming and may not provide code for side effects, like my use case?