Well, without doing anything like monad transformers or anything, you can just put in an understanding. This will be more verbose, but no additional dependencies.
val res = (for{ file1Opt <- f1 file2Opt <- f2 } yield for { file1 <- file1Opt file2 <- file2Opt } yield combineFiles(file1, file2)) .fallbackTo(Future.successful(Some("Files not found"))) //or, alternatively, .fallbackTo(Future.successful(None))
Ultimately, the problem is that you are trying to combine Future and Option in the same for sense. It just doesn't work for the reasons that other respondents talked about. However, the attachment works great.
The disadvantage of nesting is that you get very complex data structures, which can be difficult to use elsewhere in your program. You should think about how you smooth them out, i.e. From Future[Option[String]] to all Future[String] . In your specific case, you can do something like this: res.map(_.getOrElse("")) .
Ok, maybe 2 levels of nesting are great, but you nested the most, think about smoothing this hierarchy before letting your colleagues handle it. :)
source share