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

Add missing lift combinators for ZManaged #4798

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions core/shared/src/main/scala/zio/ZManaged.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1799,6 +1799,22 @@ object ZManaged extends ZManagedPlatformSpecific {
*/
def fromFunctionM[R, E, A](f: R => ZManaged[Any, E, A]): ZManaged[R, E, A] = flatten(fromFunction(f))

/**
* Lifts an `Option` into a `ZManaged` but preserves the error as an option in the error channel, making it easier to compose
* in some scenarios.
*/
def fromOption[A](v: => Option[A]): ZManaged[Any, Option[Nothing], A] =
effectTotal(v).flatMap(_.fold[Managed[Option[Nothing], A]](fail(None))(succeedNow))

/**
* Lifts a `Try` into a `ZManaged`.
*/
def fromTry[A](value: => scala.util.Try[A]): TaskManaged[A] =
effect(value).flatMap {
case scala.util.Success(v) => succeedNow(v)
case scala.util.Failure(t) => fail(t)
}

/**
* Returns an effect that models failure with the specified `Cause`.
*/
Expand Down