Coder Social home page Coder Social logo

cats-effect-cps's Introduction

cats-effect-cps

Discord

This is a prototype library for async/await syntax in Cats Effect, currently targeting Scala 2's native -Xasync support and building on dotty-cps-async for Scala 3. The async/await functionality within the Scala compiler is itself quite experimental, and thus this library should also be considered experimental until upstream support stabilizes. Once that happens and this implementation is considered to be finalized, the functionality in this library will be folded into Cats Effect itself and this library will be archived.

"CPS" stands for "Continuation Passing Style". This functionality is quite similar to similar functionality in JavaScript, Rust, Kotlin, and many other languages. The primary difference being that, in this library, the async marker is a lexical block, whereas in other languages the marker is usually a modifier applied at the function level.

Special thanks to Jason Zaugg for his work on the implementation of -Xasync within scalac. Also Ruslan Shevchenko for his work on dotty-cps-async.

Usage

libraryDependencies += "org.typelevel" %% "cats-effect-cps" % "<version>"

// if on Scala 2
scalacOptions += "-Xasync"  // required to enable compiler support on Scala 2

Published for Scala 2.13, 2.12, and 3.0, cross-build with ScalaJS 1.8. Depends on Cats Effect 3.3.0 or higher. Scala 3 support depends on dotty-cps-async 0.8.1.

Example

Consider the following program written using a for-comprehension (pretend talkToServer and writeToFile exist and do the obvious things, likely asynchronously):

import cats.effect._

for {
  results1 <- talkToServer("request1", None)
  _ <- IO.sleep(100.millis)
  results2 <- talkToServer("request2", Some(results1.data))

  back <- if (results2.isOK) {
    for {
      _ <- writeToFile(results2.data)
      _ <- IO.println("done!")
    } yield true
  } else {
    IO.println("abort abort abort").as(false)
  }
} yield back

Using cats-effect-cps, we can choose to rewrite the above in the following direct style:

import cats.effect.cps._

async[IO] {
  val results1 = talkToServer("request1", None).await
  IO.sleep(100.millis).await

  val results2 = talkToServer("request2", Some(results1.data)).await

  if (results2.isOK) {
    writeToFile(results2.data).await
    IO.println("done!").await
    true
  } else {
    IO.println("abort abort abort").await
    false
  }
}

There are no meaningful performance differences between these two encodings. They do almost exactly the same thing using different syntax, similar to how for-comprehensions are actually flatMap and map functions under the surface.

cats-effect-cps's People

Contributors

djspiewak avatar typelevel-steward[bot] avatar scala-steward avatar baccata avatar armanbilge avatar rssh avatar alexandru avatar kamilkloch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.