-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-enable macro annotations (for IntelliJ). Re-enable manifest. Add d…
…ocs. (#44) * Cleanup and more testing * Re-enable compat-generator & improve. Add scripts to enable annotation. * Add docs for ZStream/ZPure and Future/List * One more note
- Loading branch information
1 parent
afe0f37
commit 5b3827b
Showing
7 changed files
with
213 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
sed -i 's/@scala.reflect.macros.internal.macroImpl("nothing")/\/\/ @scala.reflect.macros.internal.macroImpl("nothing")/' zio-direct/src/main/scala-3.x/zio/direct/Dsl.scala |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
sed -i 's/\/\/ @scala.reflect.macros.internal.macroImpl("nothing")/@scala.reflect.macros.internal.macroImpl("nothing")/' zio-direct/src/main/scala-3.x/zio/direct/Dsl.scala |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
--- | ||
id: other-supported-monads | ||
title: "Other Supported Monads" | ||
sidebar_label: "Other Supported Monads" | ||
--- | ||
|
||
As of RC5, zio-direct now supports ZStream and ZPure as well as `scala.concurrent.Future` and `scala.List`. The latter two are largely fully functional but largely for demonstration purposes. | ||
> Note that all of these are currently only supported in Scala 3. | ||
## ZStream | ||
|
||
To use zio-direct with ZStream, add the following to your `build.sbt` file. | ||
```scala | ||
libraryDependencies += "dev.zio" %% "zio-direct-streams" % "@VERSION@" | ||
``` | ||
|
||
You can then use zio-direct with ZStream: | ||
```scala | ||
import zio.direct.stream._ | ||
|
||
val out = | ||
defer { | ||
val a = ZStream(1, 2, 3).each | ||
val b = ZStream("foo", "bar").each | ||
(a, b) | ||
} | ||
|
||
out.runCollect | ||
// ZIO.succeed(Chunk((1,"foo"),(1,"bar"),(2,"foo"),(2,"bar"),(3,"foo"),(3,"bar"))) | ||
``` | ||
|
||
Note that if you are also using zio-direct with ZIO, you should rename the `defer` function to avoid conflicts: | ||
|
||
```scala | ||
import zio.direct.stream.{ defer => deferStream, _ } | ||
import zio.direct._ | ||
|
||
// The `run` function of ZStream is called `each` | ||
val outStream: ZStream[Any, Nothing, (Int, String)] = | ||
deferStream { | ||
val a = ZStream(1, 2, 3).each | ||
val b = ZStream("foo", "bar").each | ||
(a, b) | ||
} | ||
|
||
val outZio: ZIO[Any, Nothing, Chunk[(Int, String)]] = | ||
defer { | ||
val a: Chunk[(Int, String)] = outStream.runCollect.run | ||
val b = ZIO.succeed((123, "baz")).run | ||
a :+ b | ||
} | ||
|
||
// Yields: | ||
// ZIO.succeed(Chunk((1,"foo"),(1,"bar"),(2,"foo"),(2,"bar"),(3,"foo"),(3,"bar"),(123, "baz"))) | ||
``` | ||
|
||
## ZPure | ||
|
||
> Note that Metals auto-complete/type-info popups may be sluggish when using ZPure, especially when try/catch constructs are being used. | ||
> In some cases, you may need to wait for a "Loading..." popup message for 20-30 seconds although the actual (bloop) compile time | ||
> will just be a few seconds. | ||
To use zio-direct with ZPure, add the following to your `build.sbt` file. | ||
```scala | ||
libraryDependencies += "dev.zio" %% "zio-direct-pure" % "@VERSION@" | ||
``` | ||
|
||
In order to use zio-direct with ZPure, you first need to define a `deferWith[W, S]` context which will define the Logging (`W`) and State (`S`) types for your ZPure computation. | ||
|
||
> Due to limitations of Scala 3, you may need to create the state object type in a separate file (or you may get cyclical-dependency compile-time errors). | ||
```scala | ||
val dc = deferWith[String, MyState] | ||
import dc._ | ||
|
||
// The `run` function of ZStream is called `eval` | ||
val out = | ||
defer { | ||
val s1 = ZPure.get[MyState].eval.value | ||
ZPure.set(MyState("bar")).eval | ||
val s2 = ZPure.get[MyState].eval.value | ||
(s1, s2) | ||
} | ||
|
||
out.provideState(MyState("foo")).run | ||
// ("foo", "bar") | ||
``` | ||
|
||
In order to avoid having to specify the state-type over and over again, several helpers are provided (they are imported from `dc._`). | ||
```scala | ||
val dc = deferWith[String, MyState] | ||
import dc._ | ||
|
||
// The `run` function of ZStream is called `eval` | ||
val out = | ||
defer { | ||
val s1 = getState().value | ||
setState(MyState("bar")) | ||
val s2 = getState().value | ||
(s1, s2) | ||
} | ||
|
||
out.provideState(MyState("foo")).run | ||
// ("foo", "bar") | ||
``` | ||
|
||
## List and Future | ||
|
||
Support for Scala's `List` and `Future` objects is provided from zio-direct. | ||
|
||
To use zio-direct with `List` do the following: | ||
```scala | ||
import zio.direct.list._ | ||
|
||
val out = | ||
defer { | ||
val a = List(1, 2, 3) | ||
val b = List("foo", "bar") | ||
(a, b) | ||
} | ||
|
||
// Yields: | ||
// List((1,"foo"),(1,"bar"),(2,"foo"),(2,"bar"),(3,"foo"),(3,"bar")) | ||
``` | ||
|
||
To use zio-direct with `Future` do the following: | ||
```scala | ||
import zio.direct.future._ | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
val out = | ||
defer { | ||
Future("a").run match { | ||
case "a" => Future(1).run | ||
case "b" => Future(2).run | ||
} | ||
} | ||
|
||
// Yields: Future(1) | ||
``` | ||
|
||
Note that it is not necessary to implement ExecutionContext.Implicits.global. You can | ||
implicitly pass in any ExecutionContext you want. It just needs to be in-scope when you | ||
call the `defer` function (i.e. `zio.direct.future.defer`). | ||
```scala | ||
import zio.direct.future._ | ||
|
||
def out(implicit ctx: ExecutionContext) = | ||
defer { | ||
Future("a").run match { | ||
case "a" => Future(1).run | ||
case "b" => Future(2).run | ||
} | ||
} | ||
|
||
out(scala.concurrent.ExecutionContext.global) | ||
// Yields: Future(1) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters