Skip to content

Commit

Permalink
get, getOrElse, getOrElseWith
Browse files Browse the repository at this point in the history
  • Loading branch information
sideeffffect committed Nov 13, 2021
1 parent 586b3a4 commit c3a5847
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion core/shared/src/main/scala/zio/prelude/ZValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,23 @@ sealed trait ZValidation[+W, +E, +A] { self =>
case Success(w, _) => w
}

/**
* Returns the value, if successful, or the provided `fallback` value.
*/
final def getOrElse[A1 >: A](fallback: => A1): A1 =
self match {
case Failure(_, _) => fallback
case Success(_, a) => a
}

/**
* Returns the successful value or handles the errors that have accumulated.
*/
final def getOrElseWith[A1 >: A](f: NonEmptyChunk[E] => A1): A1 = fold(f, identity)
final def getOrElseWith[A1 >: A](f: NonEmptyChunk[E] => A1): A1 =
self match {
case Failure(_, e) => f(e)
case Success(_, a) => a
}

/**
* Writes an entry to the log.
Expand Down

0 comments on commit c3a5847

Please sign in to comment.