scala - How to handle an optional value in a for-comp when dealing with Futures -


i have compr looks like:

for {   user <- getfutureuser(1) // future[user]   account <- getfutureaccount(1) // future[account]   historyopt <- ???  } yield wrapper(user, account, historyopt)  case class wrapper(user: user, account: account, historyopt: option[history]) 

i stuck on how handle historyopt value. have method history:

def gethistory(id: int): future[history] 

but based on user, if user.gethistory true, return it, otherwise none.

i tried wrong:

for {   user <- getfutureuser(1) // future[user]   account <- getfutureaccount(1) // future[account]   historyopt <- if(user.gethistory) getfuturehistory(1) else future.successful(none)  } yield wrapper(user, account, historyopt) 

how can handle scenerio?

quick answer:

case class user(gethistory: boolean) case class account() case class history()  def getfutureuser(x: int): future[user] = ??? def getfutureaccount(x: int): future[account] = ??? def getfuturehistory(x: int): future[history] = ???  case class wrapper(user: user, account: account, historyopt: option[history])  val a: future[wrapper] = {   user <- getfutureuser(1) // future[user]   account <- getfutureaccount(1) // future[account]   historyopt <- if(user.gethistory) getfuturehistory(1).map(some.apply) else future.successful(none) } yield wrapper(user, account, historyopt) 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -