Skip to content

Commit

Permalink
IOLocal - generalize scope function
Browse files Browse the repository at this point in the history
  • Loading branch information
iRevive committed Feb 4, 2023
1 parent 7507b31 commit 46da76d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
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

0 comments on commit 46da76d

Please sign in to comment.