typelevel / cats-effect-cps Goto Github PK
View Code? Open in Web Editor NEWAn incubator project for async/await syntax support for Cats Effect
License: Apache License 2.0
An incubator project for async/await syntax support for Cats Effect
License: Apache License 2.0
It would be great, if there were support for using Resource
with cats-effect-cps
. Could look something like this
import cats.effect.cps._
async[IO] {
val results1 = talkToServer("request1", None).await
IO.sleep(100.millis).await
// openResource: (String, Option[Data]) => Resource[IO, Data]
val results2 = openResource("request2", Some(results1.data)).awaitResource
if (results2.isOK) {
writeToFile(results2.data).await
IO.println("done!").await
true
} else {
IO.println("abort abort abort").await
false
}
}
awaitResource
is pretty bad name for this, but can't think of anything better at the moment. Over at dotty-cps-async, I recommended !
for IO and ~
for Resource: rssh/dotty-cps-async#43 (comment)
The motivation is that using Resource is a bit unergonomic, especially if there are many of them, because it requires the programmer to nest the blocks in use
.
Related discussion about Resource at rssh/cps-async-connect#2 (comment)
/cc @rssh
Since doobie.ConnectionIO
just can provide summon[Sync[ConnectionIO]]
, so I can't simply use cats-effect-cps in ConnectionIO
. Is there any support if possible?
It would be helpful to downstream consumers if we could provide Scala 2 and Scala 3 support in a single API.
val resource1 = async[Resource[IO, *]] {
r1.await
}
val resource2 = async[Resource[IO, *]] {
r2.await
}
val http4sServerResource = for {
r1 <- resource1
r2 <- resource2
} yield r1.exec(r2)
def run(args: List[String]): IO[ExitCode] = http4sServerResource.useForever.as(ExitCode.Success)
It seems that resource1
and resource2
is already use
when the block of async { } is ended. So when r1.exec(r2)
the resource is closed.
And in Scala3 r1.exec(r2)
can exec succeed.
I initially tried out this library with success in an CE3 IO
based project.
In some modules I use monad stacks such as WriterT
over IO
(with cats-mtl).
When I try to use a stack with await
, I get a compile error about
/* missing */
[error] | summon[
[error] | cps.CpsMonadContext[
[error] | [_] =>>
[error] | cats.data.WriterT[cats.effect.IO,
[error] | cats.data.Chain[LogRecord], _]
[error] | ]
[error] | ]
Im confident that Async
is available for WriterT
over IO
, but Im unsure if I should expect CpsMonadContext
to be derived for a monad stack? I note the readme example is for just plain IO
.
I think the following won't compile:
val a = async(io.await)
val b = async(io.await)
Since we use the same type name for everything. Macros may be doing some clever sanitization thing though which saves us.
(Re-opening typelevel/cats-effect#2039).
val dsl = AsyncAwaitDsl[IO]
import dsl._
This is a bit more uniform with http4s, and also has the advantage of being more concise.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.