scala - check optional field to populate another field -


is there efficient way remove below if , else condition

object currency {    sealed trait myconstants extends mytrait[string]   case object extends myconstants {val value ="abc"}   case object b extends myconstants {val value = "def"}  }  case class test(name:string) case class mycurerncy(currency: option[myconstants])  def check (name:string):option[test] ={.....}  if(check("xyz").isdefined){     mycurerncy(option(currency.a))  }else{     none  } 

the method check return option[test] .i need populate mycurrency based on condition if test defined or not.

mycurerncy(option(currency.a)) of type mycurerncy. none of type option[nothing]. so

 if(check("xyz").isdefined){     mycurerncy(option(currency.a))  }else{     none  } 

will of type any (actually product serializable doesn't matter). sure prefer have any?

if prefer have mycurerncy i.e.

 if(check("xyz").isdefined){     mycurerncy(option(currency.a))  }else{     mycurerncy(none)  }  

you can write

mycurerncy(check("xyz").flatmap(_ => option(currency.a))) 

if anyway prefer can write wrote or match

  check("xyz") match {     case some(_) => mycurerncy(option(currency.a))     case none => none   } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -