-
Couldn't load subscription status.
- Fork 1.4k
Uncurried versions of 'bracket' and 'bracketExit' #617
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -408,14 +408,25 @@ sealed trait ZIO[-R, +E, +A] extends Serializable { self => | |
| final def bracket_[R1 <: R, E1 >: E]: ZIO.BracketAcquire_[R1, E1] = | ||
| new ZIO.BracketAcquire_(self) | ||
|
|
||
| /** | ||
| * Uncurried version. Doesn't offer curried syntax and have worse | ||
| * type-inference characteristics, but it doesn't allocate intermediate | ||
| * [[scalaz.zio.ZIO.BracketAcquire_]] and [[scalaz.zio.ZIO.BracketRelease_]] objects. | ||
| */ | ||
| final def bracket_[R1 <: R, E1 >: E, B]( | ||
| release: ZIO[R1, Nothing, _], | ||
| use: ZIO[R1, E1, B] | ||
| ): ZIO[R1, E1, B] = | ||
| ZIO.bracket(self, (_: A) => release, (_: A) => use) | ||
|
|
||
| /** | ||
| * Returns an effect that, if this effect _starts_ execution, then the | ||
| * specified `finalizer` is guaranteed to begin execution, whether this effect | ||
| * succeeds, fails, or is interrupted. | ||
| * | ||
| * Finalizers offer very powerful guarantees, but they are low-level, and | ||
| * should generally not be used for releasing resources. For higher-level | ||
| * logic built on `ensuring`, see [[ZIO#bracket]]. | ||
| * logic built on `ensuring`, see `ZIO#bracket`. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry for this one, but scaladoc finds two |
||
| */ | ||
| final def ensuring(finalizer: UIO[_]): ZIO[R, E, A] = | ||
| new ZIO.Ensuring(self, finalizer) | ||
|
|
@@ -1317,6 +1328,24 @@ trait ZIOFunctions extends Serializable { | |
| final def bracket[R >: LowerR, E <: UpperE, A](acquire: ZIO[R, E, A]): ZIO.BracketAcquire[R, E, A] = | ||
| new ZIO.BracketAcquire[R, E, A](acquire) | ||
|
|
||
| /** | ||
| * Uncurried version. Doesn't offer curried syntax and have worse type-inference | ||
| * characteristics, but guarantees no extra allocations of intermediate | ||
| * [[scalaz.zio.ZIO.BracketAcquire]] and [[scalaz.zio.ZIO.BracketRelease]] objects. | ||
| */ | ||
| final def bracket[R >: LowerR, E <: UpperE, A, A1 >: A, A2 >: A, B]( | ||
| acquire: ZIO[R, E, A], | ||
| release: A => ZIO[R, Nothing, _], | ||
| use: A => ZIO[R, E, B] | ||
| ): ZIO[R, E, B] = | ||
| Ref.make[UIO[Any]](ZIO.unit).flatMap { m => | ||
| (for { | ||
| r <- environment[R] | ||
| a <- acquire.flatMap(a => m.set(release(a).provide(r)).const(a)).uninterruptible | ||
| b <- use(a) | ||
| } yield b).ensuring(flatten(m.get)) | ||
| } | ||
|
|
||
| /** | ||
| * Acquires a resource, uses the resource, and then releases the resource. | ||
| * Neither the acquisition nor the release will be interrupted, and the | ||
|
|
@@ -1327,6 +1356,26 @@ trait ZIOFunctions extends Serializable { | |
| final def bracketExit[R >: LowerR, E <: UpperE, A](acquire: ZIO[R, E, A]): ZIO.BracketExitAcquire[R, E, A] = | ||
| new ZIO.BracketExitAcquire(acquire) | ||
|
|
||
| /** | ||
| * Uncurried version. Doesn't offer curried syntax and have worse type-inference | ||
| * characteristics, but guarantees no extra allocations of intermediate | ||
| * [[scalaz.zio.ZIO.BracketExitAcquire]] and [[scalaz.zio.ZIO.BracketExitRelease]] objects. | ||
| */ | ||
| final def bracketExit[R >: LowerR, E <: UpperE, E1 >: E, E2 >: E <: E1, A, B]( | ||
| acquire: ZIO[R, E, A], | ||
| release: (A, Exit[E1, B]) => ZIO[R, Nothing, _], | ||
| use: A => ZIO[R, E2, B] | ||
| ): ZIO[R, E2, B] = | ||
| Ref.make[UIO[Any]](ZIO.unit).flatMap { m => | ||
| (for { | ||
| r <- environment[R] | ||
| f <- acquire | ||
| .flatMap(a => use(a).fork.tap(f => m.set(f.interrupt.flatMap(release(a, _).provide(r))))) | ||
| .uninterruptible | ||
| b <- f.join | ||
| } yield b).ensuring(flatten(m.get)) | ||
| } | ||
|
|
||
| /** | ||
| * Applies the function `f` to each element of the `Iterable[A]` and | ||
| * returns the results in a new `List[B]`. | ||
|
|
@@ -1583,7 +1632,7 @@ object ZIO extends ZIO_R_Any { | |
| } | ||
| class BracketRelease_[R, E](acquire: ZIO[R, E, _], release: ZIO[R, Nothing, _]) { | ||
| def apply[R1 <: R, E1 >: E, B](use: ZIO[R1, E1, B]): ZIO[R1, E1, B] = | ||
| ZIO.bracket(acquire)(_ => release)(_ => use) | ||
| ZIO.bracket(acquire, (_: Any) => release, (_: Any) => use) | ||
| } | ||
|
|
||
| class BracketAcquire[R, E, A](acquire: ZIO[R, E, A]) { | ||
|
|
@@ -1592,13 +1641,7 @@ object ZIO extends ZIO_R_Any { | |
| } | ||
| class BracketRelease[R, E, A](acquire: ZIO[R, E, A], release: A => ZIO[R, Nothing, _]) { | ||
| def apply[R1 <: R, E1 >: E, B](use: A => ZIO[R1, E1, B]): ZIO[R1, E1, B] = | ||
| Ref.make[UIO[Any]](ZIO.unit).flatMap { m => | ||
| (for { | ||
| r <- environment[R1] | ||
| a <- acquire.flatMap(a => m.set(release(a).provide(r)).const(a)).uninterruptible | ||
| b <- use(a) | ||
| } yield b).ensuring(flatten(m.get)) | ||
| } | ||
| ZIO.bracket(acquire, release, use) | ||
| } | ||
|
|
||
| class BracketExitAcquire[R, E, A](acquire: ZIO[R, E, A]) { | ||
|
|
@@ -1611,20 +1654,8 @@ object ZIO extends ZIO_R_Any { | |
| acquire: ZIO[R, E, A], | ||
| release: (A, Exit[E1, B]) => ZIO[R, Nothing, _] | ||
| ) { | ||
| def apply[R1 <: R, E2 >: E, B1 <: B](use: A => ZIO[R1, E2, B1])(implicit ev: E2 <:< E1): ZIO[R1, E2, B1] = | ||
| Ref.make[UIO[Any]](ZIO.unit).flatMap { m => | ||
| (for { | ||
| r <- environment[R] | ||
| f <- acquire | ||
| .flatMap( | ||
| a => | ||
| use(a).fork | ||
| .tap(f => m.set(f.interrupt.flatMap((e: Exit[E2, B1]) => release(a, e.mapError(ev)).provide(r)))) | ||
| ) | ||
| .uninterruptible | ||
| b <- f.join | ||
| } yield b).ensuring(flatten(m.get)) | ||
| } | ||
| def apply[R1 <: R, E2 >: E <: E1, B1 <: B](use: A => ZIO[R1, E2, B1]): ZIO[R1, E2, B1] = | ||
| ZIO.bracketExit[R1, E, E1, E2, A, B1](acquire, release, use) | ||
| } | ||
|
|
||
| @inline | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know where to place these
if-compiles-workstests.