Let's assume an object constructed using a builder pattern.
This builder pattern would contain a build
method focusing on fields validation and then on conversion to the targeted type.
This validation could be implemented using:
Either[FailureObject, TargetObject]
typeTry[TargetObject]
(new feature from Scala 2.10)Validation[FailureObject, TargetObject]
orValidationNEL[FailureObject, TargetObject]
from scalaz library
I read that one of the main advantages of Validation
over Either
type is that Validation
can accumulate failures "out of the box".
But what about the "new" Try
way? I noticed that Try
has "monadic" methods out of the box also, like map
, flatMap
etc... what was really missing with Either type without help of Projection
.
Thus, I'd imagine each field validation method returning a Try[FieldType]
and more precisely, in case of any failure, a Try[SpecificFieldExceptionType]
; this nested one containing a String
message field and a rootCause field that could be accumulated throughout the build
method.
Using Scala 2.10, could or should Try
practice replace scalaz validation library for simple validation like builder pattern involves?
**EDIT ****
By reading Try
source code, it sounds that Try
can't accumulate several exceptions and thus is oriented fail-fast.
Even Try.flatMap
returns the potentential previous failure and so doesn't have the notion of accumulation:
def flatMap[U](f: T => Try[U]): Try[U] = this.asInstanceOf[Try[U]]
On the contrary of ValidationNEL
that handles accumulation feature.
Any confirmation?