Skip to content

Commit

Permalink
Give some love to the Validation code (#1426)
Browse files Browse the repository at this point in the history
- Create only 1 instance of `fail(())`
- Use `NonEmptyChunk.single` in `ZValidation.fail`
  • Loading branch information
guizmaii authored Nov 26, 2024
1 parent 607be8e commit a11600c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core/shared/src/main/scala/zio/prelude/ZValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ sealed trait ZValidation[+W, +E, +A] { self =>
}

object ZValidation extends LowPriorityValidationImplicits {
private val unitFailure: Validation[Unit, Nothing] = fail(())

final case class Failure[+W, +E](log: Chunk[W], errors: NonEmptyChunk[E]) extends ZValidation[W, E, Nothing] {
lazy val errorsUnordered: NonEmptyMultiSet[E] = NonEmptyMultiSet.fromIterable(errors.head, errors.tail)
Expand Down Expand Up @@ -511,7 +512,7 @@ object ZValidation extends LowPriorityValidationImplicits {
* Constructs a `ZValidation` that fails with the specified error.
*/
def fail[E](error: E): Validation[E, Nothing] =
Failure(Chunk.empty, NonEmptyChunk(error))
Failure(Chunk.empty, NonEmptyChunk.single(error))

/**
* Constructs a `ZValidation` that fails with the specified `NonEmptyChunk`
Expand All @@ -537,7 +538,7 @@ object ZValidation extends LowPriorityValidationImplicits {
* Constructs a `ZValidation` from an `Option`.
*/
def fromOption[A](value: Option[A]): Validation[Unit, A] =
value.fold[Validation[Unit, A]](fail(()))(succeed)
value.fold[Validation[Unit, A]](unitFailure)(succeed)

/**
* Constructs a `Validation` from an `Option`, failing with the error
Expand Down

0 comments on commit a11600c

Please sign in to comment.