8000 WIP: allow AsyncShift in kyo-direct by ahoy-jon · Pull Request #1197 · getkyo/kyo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

WIP: allow AsyncShift in kyo-direct #1197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion kyo-direct/shared/src/main/scala/kyo/Validate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ private[kyo] object Validate:
case _ => false
}

def validShift(tree: Tree): Boolean =
Trees.exists(expr.asTerm) {
case Apply(TypeApply(Select(s, methodName), _), List(Block(List(DefDef(_, _, _, Some(body))), _))) if body eq tree =>
s.tpe <:< TypeRepr.of[Seq[?]] && methodName == "map"
}

end validShift

Trees.traverse(expr.asTerm) {
case Apply(TypeApply(Ident("now" | "later"), _), List(qual)) =>
Trees.traverse(qual) {
Expand Down Expand Up @@ -126,7 +134,7 @@ private[kyo] object Validate:
|}""".stripMargin)}""".stripMargin.stripMargin
)

case tree @ DefDef(_, _, _, Some(body)) if !pure(body) =>
case tree @ DefDef(_, _, _, Some(body)) if !pure(body) && !validShift(body) =>
fail(
tree,
s"""Method definitions containing ${".now".cyan} are not supported inside ${"`defer`".yellow} blocks.
Expand Down
24 changes: 24 additions & 0 deletions kyo-direct/shared/src/test/scala/kyo/ShiftTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kyo

import org.scalatest.Assertions
import org.scalatest.freespec.AnyFreeSpec

class ShiftTest extends AnyFreeSpec with Assertions:
"basic" in {
val x1: Seq[Int < Any] = Seq[Int < Any](1, 2, 3)
val x2: Seq[Int] < Any = defer:
x1.map(_.now)

assert(x2.eval == Seq(1, 2, 3))
}

"error" in {
val x1: Seq[Int < Abort[String]] = Seq(1, Abort.fail("error"), 3)
val x2: Seq[Int] < Abort[String] = defer:
x1.map(_.now)

Abort.run(x2).map: result =>
assert(result == Result.fail("error"))
}

end ShiftTest
Loading
0