How to use Validation?

1,657 views
Skip to first unread message

Heiko Seeberger

unread,
Nov 30, 2010, 4:19:49 AM11/30/10
to sca...@googlegroups.com
Hi,

I wonder how to use Validation "the right way":
  • How should I extract a Success?
  • How should I extract a Failure?
Thanks,

Heiko

Company: weiglewilczek.com
Blog: heikoseeberger.name
Follow me: twitter.com/hseeberger
OSGi on Scala: scalamodules.org
Lift, the simply functional web framework: liftweb.net
Akka - Simpler Scalability, Fault-Tolerance, Concurrency & Remoting through Actors: akkasource.org

Robert Wills

unread,
Nov 30, 2010, 4:51:16 AM11/30/10
to sca...@googlegroups.com
I spent a bit of time on this question last week in response to some
comments by Jason Zaugg.

From what I can tell the main things to tools to use are map, fold and lift.

When you want to get something out of a validation regardless of
whether it is a success or a failure
(analogous to option's 'getOrElse') use fold:

scala> val s = 1.success[String]
s: scalaz.Validation[String,Int] = Success(1)

scala> val f = "fail".fail[Int]
f: scalaz.Validation[java.lang.String,Int] = Failure(fail)

scala> f.fold( x => "", _.toString)
res38: java.lang.String =

scala> s.fold( x => "", _.toString)
res39: java.lang.String = 1

Or you could just convert to an option:
scala> s.toOption.getOrElse("")
res34: Any = 1


If you need to modify the result validation if it's a success:
scala> s.map( _ + 1)
res40: scalaz.Validation[String,Int] = Success(2)

Similarly for a failure:
scala> s.fail.map( _ + ":").validation
res41: scalaz.Validation[java.lang.String,Int] = Success(1)

scala> f.fail.map( _ + ":").validation
res42: scalaz.Validation[java.lang.String,Int] = Failure(fail:)

Lastly use lift for wrapping either the success or failure in something else:
scala> s.lift[Option, Int]
res43: scalaz.Validation[String,Option[Int]] = Success(Some(1))

scala> f.fail.lift[Option, String]
res45: scalaz.Validation[Option[String],Int] = Failure(Some(fail))

Using these methods rather than pattern matching does seem to make for
cleaner code.

Hope that helps,
Rob

> --
> You received this message because you are subscribed to the Google Groups
> "scalaz" group.
> To post to this group, send email to sca...@googlegroups.com.
> To unsubscribe from this group, send email to
> scalaz+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/scalaz?hl=en.
>

Debasish Ghosh

unread,
Nov 30, 2010, 4:55:51 AM11/30/10
to sca...@googlegroups.com
Many examples in scalaz/example/src/main/scala/scalaz/example/ExampleValidation.scala .. also have a look at Tony Morris Presentation on non-breaking error handling at http://applicative-errors-scala.googlecode.com/svn/artifacts/0.6/pdf/index.pdf

Thanks.

--
You received this message because you are subscribed to the Google Groups "scalaz" group.
To post to this group, send email to sca...@googlegroups.com.
To unsubscribe from this group, send email to scalaz+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/scalaz?hl=en.
Reply all
Reply to author
Forward
0 new messages