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

cats.mtl.Local[IO, E] from IOLocal[E] #3429

Draft
wants to merge 3 commits into
base: series/3.x
Choose a base branch
from
Draft
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
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ ThisBuild / apiURL := Some(url("https://typelevel.org/cats-effect/api/3.x/"))
ThisBuild / autoAPIMappings := true

val CatsVersion = "2.9.0"
val CatsMtlVersion = "1.3.0"
val Specs2Version = "4.19.2"
val ScalaCheckVersion = "1.17.0"
val DisciplineVersion = "1.4.0"
Expand Down Expand Up @@ -445,6 +446,7 @@ lazy val core = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(
name := "cats-effect",
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-mtl" % CatsMtlVersion,
"org.typelevel" %% "scalac-compat-annotation" % ScalacCompatVersion % CompileTime
),
mimaBinaryIssueFilters ++= Seq(
Expand Down Expand Up @@ -833,7 +835,8 @@ lazy val tests: CrossProject = crossProject(JSPlatform, JVMPlatform, NativePlatf
"org.scalacheck" %%% "scalacheck" % ScalaCheckVersion,
"org.specs2" %%% "specs2-scalacheck" % Specs2Version % Test,
"org.typelevel" %%% "discipline-specs2" % DisciplineVersion % Test,
"org.typelevel" %%% "cats-kernel-laws" % CatsVersion % Test
"org.typelevel" %%% "cats-kernel-laws" % CatsVersion % Test,
"org.typelevel" %% "cats-mtl-laws" % CatsMtlVersion % Test
),
buildInfoPackage := "catseffect"
)
Expand Down
3 changes: 3 additions & 0 deletions core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,9 @@ object IO extends IOCompanionPlatform with IOLowPriorityImplicits {
case Failure(err) => raiseError(err)
}

def local[E](e: E): IO[cats.mtl.Local[IO, E]] =
IOLocal(e).map(_.toLocal)

// instances

implicit def showForIO[A: Show]: Show[IO[A]] =
Expand Down
14 changes: 13 additions & 1 deletion core/shared/src/main/scala/cats/effect/IOLocal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package cats.effect

import cats.Applicative
import cats.data.AndThen
import cats.mtl.Local

/**
* [[IOLocal]] provides a handy way of manipulating a context on different scopes.
Expand Down Expand Up @@ -243,6 +245,17 @@ sealed trait IOLocal[A] { self =>
}
}

final def toLocal: Local[IO, A] =
new Local[IO, A] {
def applicative: Applicative[IO] =
IO.asyncForIO

def ask[A2 >: A] =
self.get

def local[B](iob: IO[B])(f: A => A): IO[B] =
self.modify(e => f(e) -> e).bracket(Function.const(iob))(self.set)
Comment on lines +256 to +257
Copy link
Member Author

Choose a reason for hiding this comment

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

This could conceivably be made a method on IOLocal itself as well, with this becoming a simple delegate.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like thin instances, but if we're discouraging non-Local use of IOLocal, maybe we shouldn't make it easier to use.

}
}

object IOLocal {
Expand Down Expand Up @@ -318,5 +331,4 @@ object IOLocal {
def getAndReset: IO[A] =
underlying.get.flatMap(s => underlying.reset.as(getter(s)))
}

}
40 changes: 40 additions & 0 deletions tests/shared/src/test/scala/cats/effect/IOMtlLocalSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020-2022 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cats
package effect

import cats.mtl.Local
import cats.mtl.laws.discipline._
import org.typelevel.discipline.specs2.mutable.Discipline

import java.util.concurrent.CancellationException

class IOMtlLocalSpec extends BaseSpec with Discipline {
sequential

implicit val ticker: Ticker = Ticker()

implicit val local: Local[IO, Int] =
// Don't try this at home
unsafeRun(IO.local(0)).fold(
throw new CancellationException("canceled"),
throw _,
_.get
)

checkAll("Local[IO, Int]", LocalTests[IO, Int].local[Int, Int])
}