Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZValidation: add get, getOrElse, getOrElseWith #802

Merged
merged 3 commits into from
Nov 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions core/shared/src/main/scala/zio/prelude/ZValidation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ sealed trait ZValidation[+W, +E, +A] { self =>
case Success(_, a) => success(a)
}

/**
* Returns the value, because no error has occurred.
*/
final def get(implicit ev: E <:< Nothing): A = self.asInstanceOf[Success[W, A]].value

/**
* Returns the value of the log.
*/
Expand All @@ -117,6 +122,24 @@ 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 =
self match {
case Failure(_, e) => f(e)
case Success(_, a) => a
}

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