diff --git a/core/jvm/src/main/scala/cats/effect/IOLocalPlatform.scala b/core/jvm/src/main/scala/cats/effect/IOLocalPlatform.scala index 6b03d041bc..4a66d31b23 100644 --- a/core/jvm/src/main/scala/cats/effect/IOLocalPlatform.scala +++ b/core/jvm/src/main/scala/cats/effect/IOLocalPlatform.scala @@ -21,11 +21,12 @@ import IOFiberConstants.ioLocalPropagation private[effect] trait IOLocalPlatform[A] { self: IOLocal[A] => /** - * Returns a [[java.lang.ThreadLocal]] that allows to unsafely get, set, and remove (aka - * reset) the value. The system property `cats.effect.ioLocalPropagation` must be `true`, - * otherwise throws an [[java.lang.UnsupportedOperationException]]. + * Returns a [[java.lang.ThreadLocal]] view of this [[IOLocal]] that allows to unsafely get, + * set, and remove (aka reset) the value in the currently running fiber. The system property + * `cats.effect.ioLocalPropagation` must be `true`, otherwise throws an + * [[java.lang.UnsupportedOperationException]]. */ - def unsafeToThreadLocal(): ThreadLocal[A] = if (ioLocalPropagation) + def unsafeThreadLocal(): ThreadLocal[A] = if (ioLocalPropagation) new ThreadLocal[A] { override def get(): A = { val fiber = IOFiber.currentIOFiber() diff --git a/tests/jvm/src/test/scala/cats/effect/unsafe/IOLocalsSpec.scala b/tests/jvm/src/test/scala/cats/effect/unsafe/IOLocalsSpec.scala index 97c06e7047..f425e67fa2 100644 --- a/tests/jvm/src/test/scala/cats/effect/unsafe/IOLocalsSpec.scala +++ b/tests/jvm/src/test/scala/cats/effect/unsafe/IOLocalsSpec.scala @@ -22,14 +22,14 @@ class IOLocalsSpec extends BaseSpec { "IOLocals" should { "return a default value" in real { IOLocal(42) - .flatMap(local => IO(local.unsafeToThreadLocal().get())) + .flatMap(local => IO(local.unsafeThreadLocal().get())) .map(_ must beEqualTo(42)) } "return a set value" in real { for { local <- IOLocal(42) - threadLocal <- IO(local.unsafeToThreadLocal()) + threadLocal <- IO(local.unsafeThreadLocal()) _ <- local.set(24) got <- IO(threadLocal.get()) } yield got must beEqualTo(24) @@ -37,13 +37,13 @@ class IOLocalsSpec extends BaseSpec { "unsafely set" in real { IOLocal(42).flatMap(local => - IO(local.unsafeToThreadLocal().set(24)) *> local.get.map(_ must beEqualTo(24))) + IO(local.unsafeThreadLocal().set(24)) *> local.get.map(_ must beEqualTo(24))) } "unsafely reset" in real { for { local <- IOLocal(42) - threadLocal <- IO(local.unsafeToThreadLocal()) + threadLocal <- IO(local.unsafeThreadLocal()) _ <- local.set(24) _ <- IO(threadLocal.remove()) got <- local.get