Skip to content

Commit de084cb

Browse files
authored
Merge pull request #554 from yadavan88/cats-effect-upgrade
Updated cats effect to latest and fixed compilation error
2 parents 49005b9 + ecae12a commit de084cb

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ lazy val scala3_lang_2 = (project in file("scala3-lang-2")).settings(
364364
lazy val cats_effects = (project in file("cats-effects"))
365365
.settings(
366366
name := "cats-effects",
367-
libraryDependencies += "org.typelevel" %% "cats-effect" % "3.3.13",
367+
libraryDependencies += catsEffect,
368368
libraryDependencies += "org.typelevel" %% "munit-cats-effect-3" % "1.0.7" % Test,
369369
libraryDependencies += "junit" % "junit" % "4.13.2" % Test
370370
)

cats-effects/src/main/scala/com/baeldung/scala/fibers/Fibers.scala

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import scala.util.Random
1010
object Fibers extends IOApp.Simple {
1111

1212
val sequentialOps =
13-
IO("Step1").debug >> IO("Step2").debug >> IO("Step3").debug
13+
IO("Step1").printIO >> IO("Step2").printIO >> IO("Step3").printIO
1414

1515
val io: IO[String] =
16-
IO("Starting a task").debug >> IO.sleep(400.millis) >> IO(
16+
IO("Starting a task").printIO >> IO.sleep(400.millis) >> IO(
1717
"Task completed"
18-
).debug
18+
).printIO
1919

2020
val fibExec = for {
2121
fib <- io.start
@@ -24,39 +24,39 @@ object Fibers extends IOApp.Simple {
2424

2525
val fibCancel: IO[Outcome[IO, Throwable, String]] = for {
2626
fib <- io.start
27-
_ <- IO.sleep(100.millis) >> fib.cancel >> IO("Fiber cancelled").debug
27+
_ <- IO.sleep(100.millis) >> fib.cancel >> IO("Fiber cancelled").printIO
2828
res <- fib.join
2929
} yield res
3030

3131
val outcome: IO[String] = fibCancel.flatMap {
32-
case Outcome.Succeeded(fa) => IO("fiber executed successfully").debug
33-
case Outcome.Errored(e) => IO("error occurred during fiber execution").debug
34-
case Outcome.Canceled() => IO("fiber was canceled!").debug
32+
case Outcome.Succeeded(fa) => IO("fiber executed successfully").printIO
33+
case Outcome.Errored(e) => IO("error occurred during fiber execution").printIO
34+
case Outcome.Canceled() => IO("fiber was canceled!").printIO
3535
}
3636

3737
val ioWithCancelationHook =
38-
io.onCancel(IO("Applying cancelation finalizer").debug.void)
38+
io.onCancel(IO("Applying cancelation finalizer").printIO.void)
3939
val finaliserAction = for {
4040
fib <- ioWithCancelationHook.start
41-
_ <- IO.sleep(100.millis) >> fib.cancel >> IO("fiber cancelled").debug
41+
_ <- IO.sleep(100.millis) >> fib.cancel >> IO("fiber cancelled").printIO
4242
_ <- fib.join
4343
} yield ()
4444

4545
val participant1 =
46-
IO("Start Task1").debug >> IO.sleep(Random.nextInt(1000).millis) >> IO(
46+
IO("Start Task1").printIO >> IO.sleep(Random.nextInt(1000).millis) >> IO(
4747
"Task 1 completed"
48-
).debug
48+
).printIO
4949
val participant2 =
50-
IO("Start Task2").debug >> IO.sleep(Random.nextInt(1000).millis) >> IO(
50+
IO("Start Task2").printIO >> IO.sleep(Random.nextInt(1000).millis) >> IO(
5151
"Task 2 completed"
52-
).debug
52+
).printIO
5353
val raceResult: IO[Either[String, String]] =
5454
IO.race(participant1, participant2)
5555

5656
val participant1WithFinaliser =
57-
participant1.onCancel(IO("Task 1 got canceled").debug.void)
57+
participant1.onCancel(IO("Task 1 got canceled").printIO.void)
5858
val participant2WithFinaliser =
59-
participant2.onCancel(IO("Task 2 got canceled").debug.void)
59+
participant2.onCancel(IO("Task 2 got canceled").printIO.void)
6060
val raceWithFinaliser =
6161
IO.race(participant1WithFinaliser, participant2WithFinaliser)
6262

@@ -67,7 +67,7 @@ object Fibers extends IOApp.Simple {
6767

6868
val ioWithTimeout: IO[String] = participant1.timeout(400.millis)
6969

70-
val ioWithFallback = participant1.timeoutTo(400.millis, IO("Fallback IO executed after timeout").debug)
70+
val ioWithFallback = participant1.timeoutTo(400.millis, IO("Fallback IO executed after timeout").printIO)
7171

7272
override def run: IO[Unit] = ioWithFallback.void
7373
}

cats-effects/src/main/scala/com/baeldung/scala/fibers/IOExtensions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import cats.effect.IO
44

55
object IOExtensions {
66
implicit class Xtensions[A](io: IO[A]) {
7-
def debug: IO[A] =
7+
def printIO: IO[A] =
88
for {
99
a <- io
1010
_ = println(s"[${Thread.currentThread().getName}] " + a)

cats-effects/src/test/scala/com/baeldung/scala/fibers/FibersUnitTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class FibersUnitTest extends CatsEffectSuite {
1515
test("Fiber should be canceled") {
1616
val fibCancel: IO[Outcome[IO, Throwable, String]] = for {
1717
fib <- io.start
18-
_ <- IO.sleep(100.millis) >> fib.cancel >> IO("Fiber cancelled").debug
18+
_ <- IO.sleep(100.millis) >> fib.cancel >> IO("Fiber cancelled").printIO
1919
res <- fib.join
2020
} yield res
2121

0 commit comments

Comments
 (0)