scala - throw Throwable within \/.fromTryCatch - and align method types correctly -


i have following existing method:

def test: \/[throwable, person] = {   \/.fromtrycatch {     //existing logic has potential throw exception (from calls .toint) etc     //now via following comprehension want combine throwable return type \/[throwable,person]     {       p <- q.firstoption.\/>(new throwable("can't find record person id = " + id))     } yield p   } } 

which produces following error:

type mismatch; found : scalaz.\/[throwable,person] (which expands to) scalaz.\/[throwable,person] required:person 

how align return type of for-comprehension type of def test?

fromtrycatch expects value evaluates person, may throw exception.

so don't need wrap exception in \/, can throw exception in fromtrycatch block , caught , wrapped automatically.

alternatively can drop fromtrycatch, since you're materializing exceptions in \/[throwable, person], e.g.

def test : \/[throwable,person] =   {     p <- q.firstoption.\/>(new throwable(s"can't find record person id = $id))   } yield p 

if instead need mix two, can

def test : \/[throwable,person] = \/.fromtrycatch {   maythrow()    val res = {     p <- q.firstoption.\/>(new throwable("can't find record person id ="+id))   } yield p    res match {     case \/-(p) => p     case -\/(e) => throw e   } } 

or better, can separate scopes , use fromtrycatch when necessary, like

def test : \/[throwable,person] =   {     _ <- \/.fromtrycatch { maythrow() }      p <- q.firstoption.\/>(new throwable("can't find record person id ="+id))   } yield p 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -