overly restrictive signature for Validation.swapped?

18 views
Skip to first unread message

Alec Zorab

unread,
Oct 22, 2012, 7:28:07 AM10/22/12
to sca...@googlegroups.com
This is probably two questions, one of which is "How am I meant to do this?" and the other is "If I got that first part right, why doesn't this work?"

So I have a function which is provided by a particularly charmless Java library, which is prone to both throwing exceptions and returning null:

def h = ??? //danger, here be dragons!

ultimately I want two functions called something like

trait ResultThing
def f: Validation[Throwable, ResultThing] = for {
    opt <- Validation fromTryCatch Option(h())
    res <- opt toSuccess (new Throwable("it wasn't there!"))
  } yield res

def g: Validation[String, ResultThing] = 
  f.[INSERT HERE TO EXTRACT MESSAGE FROM THROWABLE]

and before we get into it too much, the throwable is to placate Akka and Try.\

so in scalaz 6 I could flip to fail projection and map, then flip back, which was fine.

In scalaz7 I tried:

f.swapped(_.map(_.getMessage))

but this returns a Validation[Serialisable, ReturnType] because swappable assumes that you're returning a supertype of E.

so I'm stuck with 

f.swap.map(_.getMessage).swap

which is exactly what I though swappable was probably there to prevent.

So my questions are:

Is there a better way to write f such that I'm not grubbing around flatmapping validations etc?
Is there a better way to map failures when you're not mapping to a supertype of E?


Reply all
Reply to author
Forward
0 new messages