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

surround and surroundK added to Resource #1368

Merged
merged 3 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions core/shared/src/test/scala/cats/effect/ResourceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,25 @@ class ResourceSpec extends BaseSpec with ScalaCheck with Discipline {
// }
// }

"surround - should wrap an effect in a usage and ignore the value produced by resource" in ticked {
implicit ticker =>
pkowalcze marked this conversation as resolved.
Show resolved Hide resolved
val r = Resource.liftF(IO.pure(0))
val surroundee = IO(1)
val surrounded = r.surround(surroundee)

surrounded eqv surroundee
}

"surroundK - should wrap an effect in a usage, ignore the value produced by resource and return FunctionK" in ticked {
implicit ticker =>
val r = Resource.liftF(IO.pure(0))
val surroundee = IO(1)
val surround = r.surroundK
val surrounded = surround(surroundee)

surrounded eqv surroundee
}

}

{
Expand Down
14 changes: 14 additions & 0 deletions kernel/shared/src/main/scala/cats/effect/kernel/Resource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,20 @@ sealed abstract class Resource[+F[_], +A] {
*/

private[effect] def invariant: Resource.InvariantResource[F0, A]

/**
* Wraps an effect in a usage and ignores the value produced by resource.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change this scaladoc to something like:

acquires the resource, runs `gb`, and closes the resource once `gb` terminates, fails or gets interrupted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍 And for surroundK would the following be ok?

Creates a FunctionK that can run `gb` within a resource, which is then closed once `gb` terminates, fails or gets interrupted

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the scaladoc

*/
def surround[G[x] >: F[x]: Resource.Bracket, B >: A](gb: G[B]): G[B] =
use(_ => gb)

/**
* Creates a FunctionK that wraps an effect in a usage when applied. Value produced by the resource is ignored.
*/
def surroundK[G[x] >: F[x]: Resource.Bracket]: G ~> G =
new (G ~> G) {
override def apply[B](gb: G[B]): G[B] = use(_ => gb)
kubukoz marked this conversation as resolved.
Show resolved Hide resolved
}
}

object Resource extends ResourceInstances with ResourcePlatform {
Expand Down