-
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.
Remove implicits from typeclasses (for now), add tests and fix various (
#41) * Remove implicits from typeclasses (for now), add tests and fix various * Fixing tests and Scala 2 impls * Slight refactoring, fixing Scala 2
- Loading branch information
1 parent
cb3d8ff
commit 6f7b4bc
Showing
42 changed files
with
1,329 additions
and
344 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
56 changes: 56 additions & 0 deletions
56
zio-direct-pure/src/main/scala-3.x/zio/direct/pure/PureDsl.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,56 @@ | ||
package zio.direct.pure | ||
|
||
import zio.direct.deferCall | ||
import zio.direct.directRunCall | ||
import zio.direct.directGetCall | ||
import zio.direct.directSetCall | ||
import zio.direct.directLogCall | ||
import zio.prelude.fx.ZPure | ||
import zio.direct.core.NotDeferredException | ||
|
||
// Using this plainly can possibly case a cyclical macro error? Not sure why | ||
// class deferWithParams[W, S] extends deferCall[[R, E, A] =>> ZPure[W, S, S, R, E, A], ZPure[?, ?, ?, ?, ?, ?], S, W] | ||
// object deferWithParams { | ||
// def apply[W, S] = new deferWithParams[W, S] | ||
// } | ||
|
||
class deferWith[W, S] { | ||
object defer extends deferCall[[R, E, A] =>> ZPure[W, S, S, R, E, A], ZPure[?, ?, ?, ?, ?, ?], S, W, PureMonadModel]( | ||
zpureMonadSuccess[W, S], | ||
Some(zpureMonadFallible[W, S]), // MUCH better perf when this is removed | ||
zpureMonadSequence[W, S], | ||
zpureMonadSequencePar[W, S], | ||
Some(zpureMonadState[W, S]), | ||
Some(zpureMonadLog[W, S]) | ||
) | ||
object State { | ||
// Note that initially it was attempted to implement these things using `transparent inline def` | ||
// (just `inline def` does not work) however that implementation significantly slowed down | ||
// auto-completion speed or Metals dialog so instead the annotation method was introduced. | ||
// Also this method should have a similar annotation in Scala-2. | ||
|
||
/** Helper method to set the state */ | ||
@directSetCall | ||
def set(s: S): Unit = ZPure.set(s).eval | ||
|
||
/** Helper method to get the state */ | ||
@directGetCall | ||
def get(): S = ZPure.get[S].eval | ||
} | ||
|
||
/** Helper method to do logging */ | ||
@directLogCall | ||
def log(w: W): Unit = ZPure.log(w).eval | ||
|
||
object Wrap { | ||
def succeed[T](value: T) = ZPure.succeed[S, T](value) | ||
def attempt[T](value: T) = ZPure.attempt[S, T](value) | ||
} | ||
} | ||
|
||
type ZPureProxy[R, E, A] = ZPure[_, _, _, R, E, A] | ||
|
||
extension [R, E, A](value: ZPureProxy[R, E, A]) { | ||
@directRunCall | ||
def eval: A = NotDeferredException.fromNamed("eval") | ||
} |
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
15 changes: 0 additions & 15 deletions
15
zio-direct-pure/src/main/scala-3.x/zio/direct/stream/PureDsl.scala
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
zio-direct-pure/src/test/scala-3.x/zio/direct/pure/MyState.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,5 @@ | ||
package zio.direct.pure | ||
|
||
// This needs to be in a separate file or Scala 3 will | ||
// complain about "Cyclic macro dependencies" | ||
case class MyState(value: String) |
147 changes: 147 additions & 0 deletions
147
zio-direct-pure/src/test/scala-3.x/zio/direct/pure/PureSpec.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,147 @@ | ||
package zio.direct.pure | ||
|
||
import zio.direct.DeferRunSpec | ||
import zio.test._ | ||
import zio.test.Assertion._ | ||
import zio.direct.Use | ||
import zio.direct.core.util.Messages | ||
import scala.annotation.nowarn | ||
import zio.direct.DeferRunSpec | ||
import zio.stream.ZStream | ||
import zio.Chunk | ||
import zio.prelude.fx.ZPure | ||
|
||
|
||
object PureSpec extends DeferRunSpec { | ||
val dc = deferWith[String, MyState] | ||
import dc._ | ||
val init = MyState("init") | ||
|
||
val e = new Exception("blah") | ||
val e1 = new Exception("blahblah") | ||
|
||
val spec = suite("VariaSpec")( | ||
test("Simple Sequence") { | ||
val out = | ||
deferWith[Nothing, Any].defer { | ||
val a = ZPure.succeed(1) | ||
val b = ZPure.succeed("foo") | ||
(a.eval, b.eval) | ||
} | ||
assertIsType[ZPure[Nothing, Any, Any, Any, Nothing, (Int, String)]](out) andAssert | ||
assert(out.run)(equalTo((1, "foo"))) // | ||
}, | ||
test("Simple Sequence with State") { | ||
val out = | ||
defer { | ||
val s1 = ZPure.get[MyState].eval.value | ||
val a = ZPure.succeed[MyState, String](s1).eval | ||
ZPure.set(MyState("foo")).eval | ||
val b = ZPure.succeed[MyState, String]("bar").eval | ||
val s2 = ZPure.get[MyState].eval.value | ||
(s1, a, b, s2) | ||
} | ||
|
||
assertIsType[ZPure[String, MyState, MyState, Any, Nothing, (String, String, String, String)]](out) andAssert | ||
assert(out.provideState(MyState("init")).run)(equalTo(("init", "init", "bar", "foo"))) | ||
}, | ||
test("Simple Sequence with State - using primitives and logging") { | ||
val out = // ddd | ||
defer { | ||
val s1 = State.get().value | ||
val a = ZPure.succeed[MyState, String](s1).eval | ||
log(a) | ||
State.set(MyState("foo")) | ||
val b = ZPure.succeed[MyState, String]("bar").eval | ||
log(b) | ||
val s2 = State.get().value | ||
(s1, a, b, s2) | ||
} | ||
assert(out.runAll(MyState("init")))(equalTo( | ||
(Chunk("init", "bar"), Right((MyState("foo"), ("init", "init", "bar", "foo")))) | ||
)) | ||
}, | ||
test("Impure/Impure If-statement") { | ||
val out = defer { | ||
if (ZPure.succeed[MyState, Int](2).eval == 2) | ||
val v = ZPure.succeed[MyState, String]("foo").eval | ||
State.set(MyState(v)) | ||
v | ||
else | ||
val v = ZPure.succeed[MyState, String]("bar").eval | ||
State.set(MyState(v)) | ||
v | ||
} | ||
assert(out.run(MyState("init")))(equalTo((MyState("foo"), "foo"))) | ||
}, | ||
test("Impure/Impure Pat-match") { | ||
val out = | ||
defer { | ||
ZPure.succeed[MyState, String]("a").eval match { | ||
case "a" => ZPure.succeed[MyState, Int](1).eval | ||
case "b" => ZPure.succeed[MyState, Int](2).eval | ||
} | ||
} | ||
assert(out.run(init))(equalTo((init, 1))) | ||
}, | ||
test("Try/Catch caught") { | ||
val out = | ||
defer { | ||
try { | ||
val num = ZPure.succeed[MyState, Int](1).eval | ||
if (num == 1) { | ||
throw new FooError | ||
} else { | ||
num | ||
} | ||
} catch { | ||
case _: FooError => ZPure.succeed[MyState, Int](18).eval | ||
} | ||
} | ||
assertIsType[ZPure[String, MyState, MyState, Any, FooError, Int]](out) andAssert | ||
assert(out.catchAll(e => throw e).run(init))(equalTo((init, 18))) | ||
}, | ||
test("Try/Catch NOT caught") { | ||
val fooError = new FooError | ||
val out = | ||
defer { | ||
try { | ||
val num = Wrap.succeed(3).eval | ||
if (num == 3) { | ||
throw fooError | ||
} else { | ||
num | ||
} | ||
} catch { | ||
case _: BarError => Wrap.succeed(33).eval | ||
} | ||
} | ||
assert(out.runAll(init))(equalTo( | ||
// NOTE that the Chunk() is the logging, not the init | ||
(Chunk(), Left(zio.prelude.fx.Cause(fooError))) | ||
)) | ||
}, | ||
test("Throw-fail") { | ||
val fooError = new FooError | ||
val out = | ||
defer(Use.withNoCheck) { | ||
throw fooError | ||
} | ||
assert(out.runAll(init))(equalTo( | ||
(Chunk(), Left(zio.prelude.fx.Cause(fooError))) | ||
)) | ||
}, | ||
test("List Impure, body Impure") { | ||
var v = 1 | ||
val out = | ||
defer(Use.withLenientCheck) { | ||
for (i <- Wrap.succeed(List(1, 2, 3)).eval) { | ||
Wrap.succeed(v += i).eval | ||
} | ||
} | ||
// assert(out.provideState(init).run) doesn't run anything, possibly because the after the .run it's a Unit-type | ||
out.provideState(init).run | ||
assert(v)(equalTo(7)) | ||
} | ||
) | ||
} |
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
Oops, something went wrong.