Skip to content

marcinzh/turbolift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jan 29, 2025
3232051 · Jan 29, 2025
May 9, 2024
Jan 29, 2025
Aug 26, 2024
Jan 31, 2024
Apr 20, 2019
Jan 29, 2025
Jan 29, 2025

Repository files navigation

Maven Central Version javadoc

Turbolift: Algebraic Effects for Scala 3.

Visit the microsite for description.

See also:

Project Description
DaaE Demo: a debugger implemented as an effect
Spot Cats-Effect instances for Turbolift's IO effect
Enterprise HTTP server implemented using Turbolift's effects
Effect Zoo Microbenchmark suite for several effect systems, including Turbolift

Example

Runnable with scala-cli.

Important

Turbolift requires Java 11 or newer.

//> using scala "3.3.5"
//> using dep "io.github.marcinzh::turbolift-core:0.104.0"
import turbolift.!!
import turbolift.effects.{Reader, State, Error}

@main def main =
  case object MyReader extends Reader[Int]
  case object MyState extends State[Int]
  case object MyError extends Error[String]

  val program =
    for
      a <- MyState.get
      b <- MyReader.ask
      c <-
        if b != 0
        then !!.pure(a / b)
        else MyError.raise(s"Tried to divide $a by zero")
      _ <- MyState.put(c)
    yield ()

  val result =
    program
    .handleWith(MyState.handler(100))
    .handleWith(MyReader.handler(3))
    .handleWith(MyError.handler)
    .run

  println(result) // Right(((),33))

 

Same, but with bindless syntax extension:

//> using scala "3.3.5"
//> using dep "io.github.marcinzh::turbolift-core:0.104.0"
//> using dep "io.github.marcinzh::turbolift-bindless:0.104.0"
import turbolift.!!
import turbolift.effects.{Reader, State, Error}
import turbolift.bindless._

@main def main =
  case object MyReader extends Reader[Int]
  case object MyState extends State[Int]
  case object MyError extends Error[String]

  val program =
    `do`:
      val a = MyState.get.!
      val b = MyReader.ask.!
      val c =
        if b != 0
        then a / b
        else MyError.raise(s"Tried to divide $a by zero").!
      MyState.put(c).!

  val result =
    program
    .handleWith(MyState.handler(100))
    .handleWith(MyReader.handler(3))
    .handleWith(MyError.handler)
    .run

  println(result) // Right(((),33))

See also examples folder. Runnable with sbt:

sbt examples/run

Usage in SBT

libraryDependencies += "io.github.marcinzh" %% "turbolift-core" % "0.104.0"

Optionally, for the bindless syntax extension:

libraryDependencies += "io.github.marcinzh" %% "turbolift-bindless" % "0.104.0"