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

IOLocal - generalize scope function #3360

Merged
merged 2 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions core/shared/src/main/scala/cats/effect/IOLocal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,15 @@ sealed trait IOLocal[A] {
* for {
* local <- IOLocal(42)
* _ <- local.get // returns 42
* _ <- local.scope(0).surround(local.getAndSet(1)) // returns 0
* _ <- local.scope(current => current + 1).surround(local.getAndSet(1)) // returns 43
* _ <- local.get // returns 42, even though 1 was set inside of the resource
* } yield ()
* }}}
*
* @param value
* the value to make a scope with
* @param f
* the function to make a new scope
*/
def scope(value: A): Resource[IO, Unit]
def scope(f: A => A): Resource[IO, Unit]

}

Expand Down Expand Up @@ -256,8 +256,8 @@ object IOLocal {
override def getAndReset: IO[A] =
IO.Local(state => (state - self, getOrDefault(state)))

override def scope(value: A): Resource[IO, Unit] =
Resource.make(getAndSet(value))(p => set(p)).void
override def scope(f: A => A): Resource[IO, Unit] =
Resource.make(modify(a => (f(a), a)))(p => set(p)).void
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/shared/src/test/scala/cats/effect/IOLocalSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class IOLocalSpec extends BaseSpec {
"do not leak internal updates outside of a scope" in ticked { implicit ticker =>
val io = for {
local <- IOLocal(0)
inside <- local.scope(1).surround(local.getAndSet(2))
inside <- local.scope(_ => 1).surround(local.getAndSet(2))
outside <- local.get
} yield (inside, outside)

Expand Down