I have a series of four method calls that all return Try[Something], with the last returning Try[Unit]. Something like this:
def getFields(): Try[List[A]] = { ... }
def getValues(): Try[List[B]] = { ... }
def createTmpFile(as: List[A], bs: List[B]): Try[C] { ... }
def runProcessWithFile(c: C): Try[Unit] { ... }
def run(): Try[Unit] = {
for (
fields <- getFields();
values <- getValues();
file <- createTmpFile(fields, values);
_ <- runProcessWithFile(file)
) yield ()
}
This seems OK to me. I guess my first question is about Try[Unit]. That seems a little awkward. Option[Exception] seems equally awkward. Is there a "best practice" return type in Scala for a possible Exception?
Also, things got complicated when I wanted to add branching based on whether getValues() returns an empty list. This is what I have right now:
def run(): Try[Unit] = {
for (
fields <- getFields();
values <- getValues();
_ <- if (values.nonEmpty) {
for (
file <- createTmpFile(fields, values);
_ <- runProcessWithFile(file)
) yield ()
} else {
Success(())
}
) yield ()
}
That seems pretty hairy. Is there a better way to do this? I suppose I could abandon Try altogether and just let everything throw Exceptions, but that seems like throwing the baby out with the bathwater.