0

So my for comprehension technically compiles fine currently, but if you look carefully I have a few unsafe .get calls on the options.

for {
   maybeProduct: Option[Product] <- EitherT.liftF {
        for {
           a <- ADao.get(123).transact(xa)  // ConnectionIO[Option[A]]
           b <- BDao.get(a.get).transact(xa)      // ConnectionIO[Option[B]]
           product <- CDao.get(b.get.productId.get).transact(xa) // ConnectionIO[Option[Product]]
        } yield product
  }

  transaction <- EitherT(
        TransactionDao.insert(..., maybeProduct.map(_.id),...).transact(xa)                    
   )
} 

I need help refactoring it to remove the unsafe calls in the following lines in the for comprehension

           a <- ADao.get(123).transact(xa)  // ConnectionIO[Option[A]]
           b <- BDao.get(a.get).transact(xa)      // ConnectionIO[Option[B]]
           product <- CDao.get(b.get.productId.get).transact(xa) // ConnectionIO[Option[Product]]

Specifically the call to a.get and b.get.productId.get

7
  • And what should be the behaviour if the are None? Commented Feb 24, 2023 at 21:47
  • @LuisMiguelMejíaSuárez the final return type of the EitherT is an Option[Product] so if any of the .get calls are a None, the entire thing should return None.
    – Blankman
    Commented Feb 24, 2023 at 21:50
  • Then all you need to use is used flatTraverse and flatMapN Commented Feb 24, 2023 at 21:52
  • @LuisMiguelMejíaSuárez Where would I use both? I am not that familiar with those but I will look them up.
    – Blankman
    Commented Feb 24, 2023 at 21:58
  • Actually, looking more closely at your code, you want to use OptionT so you can just flatMap everything in the for Commented Feb 24, 2023 at 22:12

0

Browse other questions tagged or ask your own question.