I've been looking but I can't find an implicit converter. Obviously it would be trivial to write one but I'm wondering if I've missed one in the scalaz library somehow!
Asked
Active
Viewed 1,875 times
2 Answers
12
Scalaz has a implicit conversion of Option
to OptionW
, which declares the toFailure
and toSuccess
methods.

Jean-Philippe Pellet
- 59,296
- 21
- 173
- 234
-
Any chance you could provide an example? I tried this with both scalaz._ and Scalaz._ imported, and I still get "error: value toValidationNel is not a member of Option" – Raymond Barlow Jul 09 '14 at 15:09
-
Doesn't OptionOps convert for you? – Toby Sep 29 '14 at 10:26
-
1`toValidationNel` is a method usable on validation, not on option. – Daenyth Oct 03 '14 at 20:51
-
1```import scalaz.syntax.std.option._; myOption.toSuccessNel(myError)``` – Tim Sep 16 '16 at 11:56
0
Basically you have methods that transform "Some" into the "happy path" (in case of Validation -> Success, in case of Disjunction \/-) and then you need to define an error description for the None.
I use this when I receive an Optional parameter and I want to raise an error if is not provided.
Example:
scala> import scalaz.Scalaz._
import scalaz.Scalaz._
scala> import scalaz._
import scalaz._
scala> Some("clientId123").toSuccessNel("Client id is mandatory")
res0: scalaz.ValidationNel[String,String] = Success(clientId123)
scala> None.toSuccessNel("Client id is mandatory")
res1: scalaz.ValidationNel[String,Nothing] = Failure(NonEmpty[Client id is mandatory])
If you accumulate the errors using applicatives you can give a comprehensive error message to the final user, something like:
Client id is mandatory, country is mandatory, etc

Carlos Verdes
- 3,037
- 22
- 20