From 21e6fbd6e4fdfb0a689555f4bec5aa6e159bee0f Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Sat, 11 Dec 2021 21:01:45 -0800 Subject: [PATCH 1/6] Move comprehension to bangnotation --- .../com/thoughtworks/dsl/bangnotation.scala | 16 +++++++++++ build.sbt | 21 ++------------ comprehension/.js/build.sbt | 1 - comprehension/.jvm/build.sbt | 1 - comprehension/build.sbt | 26 ----------------- .../com/thoughtworks/dsl/comprehension.scala | 28 ------------------- .../com/thoughtworks/dsl/domains/Task.scala | 1 - .../com/thoughtworks/dsl/domains/scalaz.scala | 2 +- .../dsl/keywords/AsynchronousIo.scala | 1 - .../thoughtworks/dsl/keywords/AwaitTest.scala | 1 - keywords-Continue/build.sbt | 2 +- 11 files changed, 20 insertions(+), 80 deletions(-) delete mode 120000 comprehension/.js/build.sbt delete mode 120000 comprehension/.jvm/build.sbt delete mode 100644 comprehension/build.sbt delete mode 100644 comprehension/src/main/scala/com/thoughtworks/dsl/comprehension.scala diff --git a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala index 3ab7bba3c..3c7e249f1 100644 --- a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala +++ b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala @@ -720,4 +720,20 @@ object bangnotation { shift[Keyword, Value](asKeyword(from)) } + inline def map[MappedValue]( + mapper: Value => MappedValue + ): FlatMap[Keyword, Value, Pure[MappedValue]] = + FlatMap(asKeyword(from), Pure.apply.liftCo(mapper)) + + inline def flatMap[Mapped, MappedValue]( + flatMapper: Value => Mapped + )( + using /*erased*/ Dsl.AsKeyword.IsKeyword[Mapped, MappedValue] + ): FlatMap[Keyword, Value, Mapped] = + FlatMap(asKeyword(from), flatMapper) + + inline def withFilter[Mapped, MappedValue]( + filter: Value => Boolean + ) = + WithFilter(asKeyword(from), filter) } diff --git a/build.sbt b/build.sbt index c17646270..d72b33301 100644 --- a/build.sbt +++ b/build.sbt @@ -12,6 +12,7 @@ lazy val bangnotation = `keywords-Typed`, `keywords-Return`, `keywords-FlatMap`, + `keywords-WithFilter`, `keywords-Match`, `keywords-Suspend`, `keywords-Pure`, @@ -39,7 +40,6 @@ lazy val `domains-Task` = `keywords-Each` % Test, `keywords-Using` % Test, `keywords-Yield` % Test, - comprehension % Test ) lazy val `keywords-Fork` = @@ -107,7 +107,7 @@ lazy val `keywords-Return` = lazy val `keywords-Continue` = crossProject(JSPlatform, JVMPlatform) .crossType(CrossType.Pure) - .dependsOn(Dsl, bangnotation % Test, `keywords-Each` % Test) + .dependsOn(Dsl) lazy val `keywords-Get` = crossProject(JSPlatform, JVMPlatform) @@ -133,7 +133,6 @@ lazy val `keywords-AsynchronousIo` = `keywords-In` % Test, `keywords-Each` % Test, `keywords-Using` % Test, - comprehension % Test, `domains-Task` % Test ) @@ -183,7 +182,6 @@ lazy val `keywords-Await` = .dependsOn( Dsl, `domains-Continuation`, - comprehension % Test, bangnotation % Test, `domains-Task` % Test, `keywords-In` % Test, @@ -214,21 +212,6 @@ lazy val `keywords-Monadic` = .crossType(CrossType.Pure) .dependsOn(Dsl, bangnotation % Test) -lazy val comprehension = - crossProject(JSPlatform, JVMPlatform) - .crossType(CrossType.Pure) - .dependsOn( - `keywords-Map`, - `keywords-FlatMap`, - `keywords-WithFilter`, - `keywords-Return`, - `keywords-Yield` % Test, - `keywords-In` % Test, - `keywords-Using` % Test, - `keywords-Continue` % Test, - bangnotation % Test - ) - organization in ThisBuild := "com.thoughtworks.dsl" skip in publish := true diff --git a/comprehension/.js/build.sbt b/comprehension/.js/build.sbt deleted file mode 120000 index 0569694fc..000000000 --- a/comprehension/.js/build.sbt +++ /dev/null @@ -1 +0,0 @@ -../build.sbt \ No newline at end of file diff --git a/comprehension/.jvm/build.sbt b/comprehension/.jvm/build.sbt deleted file mode 120000 index 0569694fc..000000000 --- a/comprehension/.jvm/build.sbt +++ /dev/null @@ -1 +0,0 @@ -../build.sbt \ No newline at end of file diff --git a/comprehension/build.sbt b/comprehension/build.sbt deleted file mode 100644 index 19e71ff64..000000000 --- a/comprehension/build.sbt +++ /dev/null @@ -1,26 +0,0 @@ -libraryDependencies += "com.github.sbt" % "junit-interface" % "0.13.2" % Test - -libraryDependencies += "junit" % "junit" % "4.13.2" % Test - -libraryDependencies += "com.lihaoyi" %%% "utest" % "0.7.10" % Test - -testFrameworks += new TestFramework("utest.runner.Framework") - -enablePlugins(Example) - -import meta._ -exampleSuperTypes := exampleSuperTypes.value.map { - case init"_root_.org.scalatest.freespec.AnyFreeSpec" => - init"_root_.org.scalatest.freespec.AsyncFreeSpec" - case otherTrait => - otherTrait -} - -scalacOptions in Compile ++= { - scalaBinaryVersion.value match { - case "2.11" => Some("–Xexperimental") - case _ => None - } -} - -libraryDependencies += "org.scalatest" %%% "scalatest" % "3.2.10" % Test diff --git a/comprehension/src/main/scala/com/thoughtworks/dsl/comprehension.scala b/comprehension/src/main/scala/com/thoughtworks/dsl/comprehension.scala deleted file mode 100644 index 7dc8529cb..000000000 --- a/comprehension/src/main/scala/com/thoughtworks/dsl/comprehension.scala +++ /dev/null @@ -1,28 +0,0 @@ -package com.thoughtworks.dsl - -import keywords._ -import Dsl._ -import scala.collection.WithFilter - -object comprehension { - extension [From, Keyword, Value](keyword: From)(using - asKeyword: AsKeyword.SearchIsKeywordFirst[From, Keyword, Value] - ) - - @inline def map[MappedValue]( - mapper: Value => MappedValue - ): FlatMap[Keyword, Value, Pure[MappedValue]] = - FlatMap(asKeyword(keyword), Pure.apply.liftCo(mapper)) - - @inline def flatMap[Mapped, MappedValue]( - flatMapper: Value => Mapped - )( - using /*erased*/ AsKeyword.IsKeyword[Mapped, MappedValue] - ): FlatMap[Keyword, Value, Mapped] = - FlatMap(asKeyword(keyword), flatMapper) - - @inline def withFilter[Mapped, MappedValue]( - filter: Value => Boolean - ) = - WithFilter(asKeyword(keyword), filter) -} diff --git a/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala b/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala index 8ae54f576..5f6d3d4c8 100644 --- a/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala +++ b/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala @@ -25,7 +25,6 @@ import scala.util.control.TailCalls.TailRec * * {{{ * import com.thoughtworks.dsl.Dsl._ - * import com.thoughtworks.dsl.comprehension._ * import com.thoughtworks.dsl.bangnotation._ * import com.thoughtworks.dsl._ * import com.thoughtworks.dsl.keywords._ diff --git a/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala b/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala index b28553784..86462f0f4 100644 --- a/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala +++ b/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala @@ -27,7 +27,7 @@ import scala.util.control.NonFatal * import com.thoughtworks.dsl.keywords.Monadic * import com.thoughtworks.dsl.keywords.Monadic.implicitMonadic * import com.thoughtworks.dsl.domains.scalaz.given - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation.{unary_!, reset} * * val trampoline3 = Trampoline.done(3) * diff --git a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala index 346c079ae..6c1146cdd 100644 --- a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala +++ b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala @@ -16,7 +16,6 @@ import scala.util.control.NonFatal * {{{ * import java.nio._, file._, channels._ * import com.thoughtworks.dsl.bangnotation._ - * import com.thoughtworks.dsl.comprehension._ * import com.thoughtworks.dsl.domains.Task * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ diff --git a/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala b/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala index 8b19ca109..bbcc6444c 100644 --- a/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala +++ b/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala @@ -1,6 +1,5 @@ package com.thoughtworks.dsl -import comprehension._ import bangnotation._ import Dsl._ import keywords._, Match._ diff --git a/keywords-Continue/build.sbt b/keywords-Continue/build.sbt index 3e3849b57..220308e8c 100644 --- a/keywords-Continue/build.sbt +++ b/keywords-Continue/build.sbt @@ -1,4 +1,4 @@ -enablePlugins(Example) +// enablePlugins(Example) libraryDependencies += "org.scalatest" %%% "scalatest" % "3.2.10" % Test From 54aa3342fc09cb90b9417af4718903a431571665 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Sat, 11 Dec 2021 22:53:30 -0800 Subject: [PATCH 2/6] Add Keyword and OpaqueKeyword --- .../main/scala/com/thoughtworks/dsl/Dsl.scala | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala b/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala index 2d68b2969..44b6d88a6 100644 --- a/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala +++ b/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala @@ -481,6 +481,22 @@ object Dsl extends LowPriorityDsl0 { run(asKeyword(keyword)) } + /** A marker trait that denotes a keyword class, enabling extension method + * defined in [[Dsl]] for subclasses of [[Keyword]]. + */ + trait Keyword extends Any + + /** A marker trait that denotes a keyword opaque type, enabling extension + * method defined in [[Dsl]] for its subtypes of [[OpaqueKeyword]]. + */ + opaque type OpaqueKeyword = Any + object OpaqueKeyword { + opaque type Of[Self] <: Self & OpaqueKeyword = Self + object Of { + def apply[Self]: Self =:= Of[Self] = summon + } + } + trait AsKeyword[From, Keyword, Value] extends (From => Keyword) object AsKeyword { From d9298f66b62d457647982516ea679fe130c90191 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Sun, 12 Dec 2021 00:12:54 -0800 Subject: [PATCH 3/6] Let all keywords extend Dsl.Keyword or Dsl.OpaqueKeyword In the next commit, we will be able to move extension methods to Dsl --- .../dsl/keywords/AsynchronousIo.scala | 2 +- .../com/thoughtworks/dsl/keywords/Await.scala | 6 +++--- .../thoughtworks/dsl/keywords/Continue.scala | 2 +- .../com/thoughtworks/dsl/keywords/Each.scala | 2 +- .../thoughtworks/dsl/keywords/FlatMap.scala | 2 +- .../thoughtworks/dsl/keywords/ForEach.scala | 2 +- .../com/thoughtworks/dsl/keywords/Fork.scala | 2 +- .../com/thoughtworks/dsl/keywords/Get.scala | 2 +- .../com/thoughtworks/dsl/keywords/If.scala | 2 +- .../com/thoughtworks/dsl/keywords/In.scala | 4 ++-- .../com/thoughtworks/dsl/keywords/Map.scala | 3 +-- .../com/thoughtworks/dsl/keywords/Match.scala | 2 +- .../thoughtworks/dsl/keywords/Monadic.scala | 6 +++--- .../thoughtworks/dsl/keywords/NoneSafe.scala | 2 +- .../com/thoughtworks/dsl/keywords/Pure.scala | 4 ++-- .../com/thoughtworks/dsl/keywords/Put.scala | 2 +- .../com/thoughtworks/dsl/keywords/Return.scala | 4 ++-- .../com/thoughtworks/dsl/keywords/Shift.scala | 18 ++++++++++-------- .../thoughtworks/dsl/keywords/Suspend.scala | 4 ++-- .../thoughtworks/dsl/keywords/TryCatch.scala | 2 +- .../dsl/keywords/TryCatchFinally.scala | 2 +- .../thoughtworks/dsl/keywords/TryFinally.scala | 2 +- .../com/thoughtworks/dsl/keywords/Typed.scala | 4 ++-- .../com/thoughtworks/dsl/keywords/Using.scala | 4 ++-- .../com/thoughtworks/dsl/keywords/While.scala | 2 +- .../thoughtworks/dsl/keywords/WithFilter.scala | 2 +- .../com/thoughtworks/dsl/keywords/Yield.scala | 12 ++++++------ 27 files changed, 51 insertions(+), 50 deletions(-) diff --git a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala index 6c1146cdd..0c53d3146 100644 --- a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala +++ b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala @@ -80,7 +80,7 @@ import scala.util.control.NonFatal * }) * }}} */ -trait AsynchronousIo[Value] extends Any { +trait AsynchronousIo[Value] extends Any with Dsl.Keyword { /** Starts the asynchronous operations */ protected def start[Attachment](attachment: Attachment, handler: CompletionHandler[Value, _ >: Attachment]): Unit diff --git a/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala b/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala index b1db30a10..9b74fd059 100644 --- a/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala +++ b/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala @@ -106,9 +106,9 @@ import scala.language.implicitConversions * @author * 杨博 (Yang Bo) */ -opaque type Await[Result] = concurrent.Future[Result] +opaque type Await[Result] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[concurrent.Future[Result]] object Await { - @inline def apply[Result]: concurrent.Future[Result] =:= Await[Result] = summon + @inline def apply[Result]: concurrent.Future[Result] =:= Await[Result] = Dsl.OpaqueKeyword.Of.apply given [Result]: AsKeyword.IsKeyword[Await[Result], Result] with {} implicit def streamAwaitDsl[Value, That](implicit @@ -143,6 +143,6 @@ object Await { !!.fromTryContinuation[Unit, Value](keyword.onComplete)(handler) } - given implicitAwait[Value]: AsKeyword.IsKeywordSubtype[Future[Value], Await[Value], Value] with {} + given implicitAwait[Value]: AsKeyword[Future[Value], Await[Value], Value] = Await(_) } diff --git a/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala b/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala index ea571d1b7..620cdbd29 100644 --- a/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala +++ b/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala @@ -13,7 +13,7 @@ import scala.collection._ * * @see The [[Continue$ Continue]] object, which is the only instance of this [[Continue]] class. */ -sealed class Continue +sealed class Continue extends Dsl.Keyword /** A keyword to skip the current iteration in a collection comprehension block. * diff --git a/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala b/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala index 7543b0499..578a1fe52 100644 --- a/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala +++ b/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala @@ -21,7 +21,7 @@ import scala.collection.mutable.Builder * }}} * @see [[comprehension]] if you want to use traditional `for` comprehension instead of !-notation. */ -final case class Each[Element](elements: Traversable[Element]) +final case class Each[Element](elements: Traversable[Element]) extends Dsl.Keyword object Each { given [Element]: AsKeyword.IsKeyword[Each[Element], Element] with {} diff --git a/keywords-FlatMap/src/main/scala/com/thoughtworks/dsl/keywords/FlatMap.scala b/keywords-FlatMap/src/main/scala/com/thoughtworks/dsl/keywords/FlatMap.scala index 098369a50..4914d561d 100644 --- a/keywords-FlatMap/src/main/scala/com/thoughtworks/dsl/keywords/FlatMap.scala +++ b/keywords-FlatMap/src/main/scala/com/thoughtworks/dsl/keywords/FlatMap.scala @@ -8,7 +8,7 @@ import scala.util.NotGiven final case class FlatMap[Upstream, UpstreamValue, Mapped]( upstream: Upstream, flatMapper: UpstreamValue => Mapped -) +) extends Dsl.Keyword object FlatMap { given [Upstream, UpstreamValue, Mapped, MappedValue](using diff --git a/keywords-ForEach/src/main/scala/com/thoughtworks/dsl/keywords/ForEach.scala b/keywords-ForEach/src/main/scala/com/thoughtworks/dsl/keywords/ForEach.scala index 7406baa85..7eb4df66d 100644 --- a/keywords-ForEach/src/main/scala/com/thoughtworks/dsl/keywords/ForEach.scala +++ b/keywords-ForEach/src/main/scala/com/thoughtworks/dsl/keywords/ForEach.scala @@ -9,7 +9,7 @@ import scala.language.implicitConversions import scala.collection.mutable.Builder /** Iterates though each element in [[elements]]. */ -final case class ForEach[Element](elements: Traversable[Element]) +final case class ForEach[Element](elements: Traversable[Element]) extends Dsl.Keyword object ForEach { given [Element]: AsKeyword.IsKeyword[ForEach[Element], Element] with {} given [Element]: AsKeyword[Traversable[Element], ForEach[Element], Element] = ForEach(_) diff --git a/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala b/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala index d9360d729..f0680d578 100644 --- a/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala +++ b/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala @@ -13,7 +13,7 @@ import scala.collection.mutable.Builder import scala.language.implicitConversions import scala.util.control.NonFatal -final case class Fork[Element](elements: Traversable[Element]) extends AnyVal +final case class Fork[Element](elements: Traversable[Element]) extends AnyVal with Dsl.Keyword object Fork { given [Element]: AsKeyword.IsKeyword[Fork[Element], Element] with {} diff --git a/keywords-Get/src/main/scala/com/thoughtworks/dsl/keywords/Get.scala b/keywords-Get/src/main/scala/com/thoughtworks/dsl/keywords/Get.scala index cf1b1e89b..b353cd55c 100644 --- a/keywords-Get/src/main/scala/com/thoughtworks/dsl/keywords/Get.scala +++ b/keywords-Get/src/main/scala/com/thoughtworks/dsl/keywords/Get.scala @@ -7,7 +7,7 @@ import com.thoughtworks.dsl.Dsl.AsKeyword * @author * 杨博 (Yang Bo) */ -final case class Get[S]() +final case class Get[S]() extends Dsl.Keyword object Get { given [S]: AsKeyword.IsKeyword[Get[S], S] with {} diff --git a/keywords-If/src/main/scala/com/thoughtworks/dsl/keywords/If.scala b/keywords-If/src/main/scala/com/thoughtworks/dsl/keywords/If.scala index 3146c6d8c..f5a6122d7 100644 --- a/keywords-If/src/main/scala/com/thoughtworks/dsl/keywords/If.scala +++ b/keywords-If/src/main/scala/com/thoughtworks/dsl/keywords/If.scala @@ -6,7 +6,7 @@ import Dsl.cpsApply final case class If[ConditionKeyword, ThenKeyword, ElseKeyword]( cond: ConditionKeyword, thenp: ThenKeyword, - elsep: ElseKeyword) + elsep: ElseKeyword) extends Dsl.Keyword object If { given[ConditionKeyword, ThenKeyword, ElseKeyword, Domain, Value]( diff --git a/keywords-In/src/main/scala/com/thoughtworks/dsl/keywords/In.scala b/keywords-In/src/main/scala/com/thoughtworks/dsl/keywords/In.scala index b15c01edd..434c1d6be 100644 --- a/keywords-In/src/main/scala/com/thoughtworks/dsl/keywords/In.scala +++ b/keywords-In/src/main/scala/com/thoughtworks/dsl/keywords/In.scala @@ -2,9 +2,9 @@ package com.thoughtworks.dsl package keywords import Dsl.AsKeyword -opaque type In[Element] = Iterable[Element] +opaque type In[Element] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[Iterable[Element]] object In { - @inline def apply[Element]: Iterable[Element] =:= In[Element] = summon + @inline def apply[Element]: Iterable[Element] =:= In[Element] = Dsl.OpaqueKeyword.Of.apply given [Element]: AsKeyword.IsKeyword[In[Element], Element] with {} diff --git a/keywords-Map/src/main/scala/com/thoughtworks/dsl/keywords/Map.scala b/keywords-Map/src/main/scala/com/thoughtworks/dsl/keywords/Map.scala index 9e7871a3d..ea89eb982 100644 --- a/keywords-Map/src/main/scala/com/thoughtworks/dsl/keywords/Map.scala +++ b/keywords-Map/src/main/scala/com/thoughtworks/dsl/keywords/Map.scala @@ -1,8 +1,7 @@ package com.thoughtworks.dsl.keywords import com.thoughtworks.dsl.Dsl -final case class Map[UpstreamKeyword, UpstreamValue, Value](upstream: UpstreamKeyword, mapper: UpstreamValue => Value) - +final case class Map[UpstreamKeyword, UpstreamValue, Value](upstream: UpstreamKeyword, mapper: UpstreamValue => Value) extends Dsl.Keyword object Map { given [UpstreamKeyword, UpstreamValue, Value]: Dsl.AsKeyword.IsKeyword[Map[UpstreamKeyword, UpstreamValue, Value], Value] with {} diff --git a/keywords-Match/src/main/scala/com/thoughtworks/dsl/keywords/Match.scala b/keywords-Match/src/main/scala/com/thoughtworks/dsl/keywords/Match.scala index 74bb1b57f..f4baa27a8 100644 --- a/keywords-Match/src/main/scala/com/thoughtworks/dsl/keywords/Match.scala +++ b/keywords-Match/src/main/scala/com/thoughtworks/dsl/keywords/Match.scala @@ -5,7 +5,7 @@ import Dsl.AsKeyword type Match[Keyword, Value, CaseSet] = keywords.FlatMap[Keyword, Value, CaseSet] object Match { export FlatMap.apply - case class WithIndex[Index <: Int, Keyword](index: Index, keyword: Keyword) + case class WithIndex[Index <: Int, Keyword](index: Index, keyword: Keyword) extends Dsl.Keyword opaque type +:[A, B] = A | B diff --git a/keywords-Monadic/src/main/scala/com/thoughtworks/dsl/keywords/Monadic.scala b/keywords-Monadic/src/main/scala/com/thoughtworks/dsl/keywords/Monadic.scala index a0bc7265e..b6d40baf3 100644 --- a/keywords-Monadic/src/main/scala/com/thoughtworks/dsl/keywords/Monadic.scala +++ b/keywords-Monadic/src/main/scala/com/thoughtworks/dsl/keywords/Monadic.scala @@ -14,13 +14,13 @@ import scala.language.implicitConversions * @todo * [[Monadic]] should be a [[scala.AnyVal]] after [[https://github.com/scala/bug/issues/10595]] is resolved. */ -opaque type Monadic[Functor[_], Value] <: Any = Functor[Value] +opaque type Monadic[Functor[_], Value] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[Functor[Value]] object Monadic { - @inline def apply[Functor[_], Value]: Functor[Value] =:= Monadic[Functor, Value] = summon + @inline def apply[Functor[_], Value]: Functor[Value] =:= Monadic[Functor, Value] = Dsl.OpaqueKeyword.Of.apply given [Functor[_], Value]: AsKeyword.IsKeyword[Monadic[Functor, Value], Value] with {} - given implicitMonadic[Functor[_], Value]: AsKeyword.IsKeywordSubtype[Functor[Value], Monadic[Functor, Value], Value] with {} + given implicitMonadic[Functor[_], Value]: AsKeyword[Functor[Value], Monadic[Functor, Value], Value] = Monadic(_) } diff --git a/keywords-NoneSafe/src/main/scala/com/thoughtworks/dsl/keywords/NoneSafe.scala b/keywords-NoneSafe/src/main/scala/com/thoughtworks/dsl/keywords/NoneSafe.scala index 3ef80cba4..7c0e5ef25 100644 --- a/keywords-NoneSafe/src/main/scala/com/thoughtworks/dsl/keywords/NoneSafe.scala +++ b/keywords-NoneSafe/src/main/scala/com/thoughtworks/dsl/keywords/NoneSafe.scala @@ -3,7 +3,7 @@ package keywords import com.thoughtworks.dsl.Dsl.AsKeyword import scala.language.implicitConversions -final case class NoneSafe[A](option: Option[A]) extends AnyVal +final case class NoneSafe[A](option: Option[A]) extends AnyVal with Dsl.Keyword object NoneSafe { given [A]: AsKeyword.IsKeyword[NoneSafe[A], A] with {} diff --git a/keywords-Pure/src/main/scala/com/thoughtworks/dsl/keywords/Pure.scala b/keywords-Pure/src/main/scala/com/thoughtworks/dsl/keywords/Pure.scala index 3d1475e2a..2daaddaa0 100644 --- a/keywords-Pure/src/main/scala/com/thoughtworks/dsl/keywords/Pure.scala +++ b/keywords-Pure/src/main/scala/com/thoughtworks/dsl/keywords/Pure.scala @@ -3,7 +3,7 @@ package keywords import Dsl.AsKeyword -opaque type Pure[+Value] = Value +opaque type Pure[+Value] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[Value] object Pure { given [Domain, Value](using shiftDsl: Dsl.PolyCont[Shift[Domain, Value], Domain, Value] @@ -12,5 +12,5 @@ object Pure { } given [PureValue]: AsKeyword.IsKeyword[Pure[PureValue], PureValue] with {} - @inline def apply[Value]: Value =:= Pure[Value] = summon[Value =:= Pure[Value]] + @inline def apply[Value]: Value =:= Pure[Value] = Dsl.OpaqueKeyword.Of.apply } diff --git a/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala b/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala index 69eeacc7d..7fa74fe62 100644 --- a/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala +++ b/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala @@ -56,7 +56,7 @@ import com.thoughtworks.dsl.Dsl.AsKeyword * @author * 杨博 (Yang Bo) */ -final case class Put[S](value: S) extends AnyVal +final case class Put[S](value: S) extends AnyVal with Dsl.Keyword object Put { given [S]: AsKeyword.IsKeyword[Put[S], Unit] with {} diff --git a/keywords-Return/src/main/scala/com/thoughtworks/dsl/keywords/Return.scala b/keywords-Return/src/main/scala/com/thoughtworks/dsl/keywords/Return.scala index fd31cb0e2..6d82064e4 100644 --- a/keywords-Return/src/main/scala/com/thoughtworks/dsl/keywords/Return.scala +++ b/keywords-Return/src/main/scala/com/thoughtworks/dsl/keywords/Return.scala @@ -12,9 +12,9 @@ import Dsl.Lift * @author * 杨博 (Yang Bo) */ -opaque type Return[ReturnValue] = ReturnValue +opaque type Return[ReturnValue] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[ReturnValue] object Return { - @inline def apply[ReturnValue]: ReturnValue =:= Return[ReturnValue] = summon + @inline def apply[ReturnValue]: ReturnValue =:= Return[ReturnValue] = Dsl.OpaqueKeyword.Of.apply given [ReturnValue]: AsKeyword.IsKeyword[Return[ReturnValue], Nothing] with {} diff --git a/keywords-Shift/src/main/scala/com/thoughtworks/dsl/keywords/Shift.scala b/keywords-Shift/src/main/scala/com/thoughtworks/dsl/keywords/Shift.scala index c33ea8ead..becf3fac9 100644 --- a/keywords-Shift/src/main/scala/com/thoughtworks/dsl/keywords/Shift.scala +++ b/keywords-Shift/src/main/scala/com/thoughtworks/dsl/keywords/Shift.scala @@ -3,6 +3,7 @@ package keywords import Dsl.AsKeyword import com.thoughtworks.dsl.Dsl +import com.thoughtworks.dsl.Dsl.!! import com.thoughtworks.dsl.keywords.Shift.{SameDomainStackSafeShiftDsl, StackSafeShiftDsl} import scala.annotation.tailrec @@ -13,7 +14,7 @@ import scala.util.control.TailCalls.TailRec /** @author * 杨博 (Yang Bo) */ -opaque type Shift[R, A] = (A => R) => R +opaque type Shift[R, A] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[R !! A] private[keywords] trait LowPriorityShift1 { @@ -30,9 +31,12 @@ private[keywords] trait LowPriorityShift0 extends LowPriorityShift1 { this: Shif given [LeftDomain, RightDomain, Value](using restDsl: SameDomainStackSafeShiftDsl[LeftDomain, RightDomain] - ): SameDomainStackSafeShiftDsl[LeftDomain !! RightDomain, Value] = { (keyword, handler) => - keyword { value => - restDsl.cpsApply(handler(value), _) + ): SameDomainStackSafeShiftDsl[LeftDomain !! RightDomain, Value] = { + val restDsl0 = apply.liftContra[[X] =>> Dsl[X, LeftDomain, RightDomain]](restDsl); + { (keyword, handler) => + keyword { value => + restDsl0.cpsApply(handler(value), _) + } } } @@ -45,7 +49,7 @@ object Shift extends LowPriorityShift0 { private[keywords] type SameDomainStackSafeShiftDsl[Domain, Value] = StackSafeShiftDsl[Domain, Domain, Value] - given implicitShift[Domain, Value]: AsKeyword.IsKeywordSubtype[Domain !! Value, Shift[Domain, Value], Value] with {} + given implicitShift[Domain, Value]: AsKeyword[Domain !! Value, Shift[Domain, Value], Value] = Shift(_) private def shiftTailRec[R, Value](continuation: TailRec[R] !! Value, handler: Value => TailRec[R]) = { continuation { a => @@ -141,8 +145,6 @@ object Shift extends LowPriorityShift0 { taskFlatMap(keyword, handler) } - private[keywords] type !![R, +A] = (A => R) => R - - def apply[R, A]: (R !! A) =:= Shift[R, A] = summon + def apply[R, A]: (R !! A) =:= Shift[R, A] = Dsl.OpaqueKeyword.Of.apply } diff --git a/keywords-Suspend/src/main/scala/com/thoughtworks/dsl/keywords/Suspend.scala b/keywords-Suspend/src/main/scala/com/thoughtworks/dsl/keywords/Suspend.scala index 6a8e45bcf..973f777e7 100644 --- a/keywords-Suspend/src/main/scala/com/thoughtworks/dsl/keywords/Suspend.scala +++ b/keywords-Suspend/src/main/scala/com/thoughtworks/dsl/keywords/Suspend.scala @@ -3,9 +3,9 @@ package keywords import Dsl.AsKeyword import Dsl.cpsApply -opaque type Suspend[Keyword] = () => Keyword +opaque type Suspend[Keyword] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[() => Keyword] object Suspend { - @inline def apply[Keyword]: (() => Keyword) =:= Suspend[Keyword] = summon + @inline def apply[Keyword]: (() => Keyword) =:= Suspend[Keyword] = Dsl.OpaqueKeyword.Of.apply given[Upstream, UpstreamValue](using upstreamIsKeyword: => AsKeyword.IsKeyword[Upstream, UpstreamValue]): AsKeyword.IsKeyword[Suspend[Upstream], UpstreamValue] with {} diff --git a/keywords-TryCatch/src/main/scala/com/thoughtworks/dsl/keywords/TryCatch.scala b/keywords-TryCatch/src/main/scala/com/thoughtworks/dsl/keywords/TryCatch.scala index 984e0c7d5..361e110e0 100644 --- a/keywords-TryCatch/src/main/scala/com/thoughtworks/dsl/keywords/TryCatch.scala +++ b/keywords-TryCatch/src/main/scala/com/thoughtworks/dsl/keywords/TryCatch.scala @@ -7,7 +7,7 @@ import scala.util.control.Exception.Catcher import scala.concurrent._ import scala.util.control.NonFatal -case class TryCatch[BlockKeyword, CaseKeyword](block: BlockKeyword, cases: Catcher[CaseKeyword]) +case class TryCatch[BlockKeyword, CaseKeyword](block: BlockKeyword, cases: Catcher[CaseKeyword]) extends Dsl.Keyword object TryCatch { given [Value, OuterDomain, BlockKeyword, BlockDomain, CaseKeyword]( diff --git a/keywords-TryCatchFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryCatchFinally.scala b/keywords-TryCatchFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryCatchFinally.scala index 90206f762..8e424f8e6 100644 --- a/keywords-TryCatchFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryCatchFinally.scala +++ b/keywords-TryCatchFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryCatchFinally.scala @@ -8,7 +8,7 @@ case class TryCatchFinally[BlockKeyword, CaseKeyword, FinalizerKeyword]( block: BlockKeyword, cases: Catcher[CaseKeyword], finalizer: FinalizerKeyword -) +) extends Dsl.Keyword object TryCatchFinally { given [Value, OuterDomain, BlockKeyword, BlockDomain, CaseKeyword, FinalizerKeyword, FinalizerDomain]( diff --git a/keywords-TryFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryFinally.scala b/keywords-TryFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryFinally.scala index 910b4791a..6badb11c3 100644 --- a/keywords-TryFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryFinally.scala +++ b/keywords-TryFinally/src/main/scala/com/thoughtworks/dsl/keywords/TryFinally.scala @@ -10,7 +10,7 @@ import scala.util.control.NonFatal case class TryFinally[TryKeyword, FinalizerKeyword]( block: TryKeyword, finalizer: FinalizerKeyword -) +) extends Dsl.Keyword object TryFinally { diff --git a/keywords-Typed/src/main/scala/com/thoughtworks/dsl/keywords/Typed.scala b/keywords-Typed/src/main/scala/com/thoughtworks/dsl/keywords/Typed.scala index 1dd4bab87..e0071f7e0 100644 --- a/keywords-Typed/src/main/scala/com/thoughtworks/dsl/keywords/Typed.scala +++ b/keywords-Typed/src/main/scala/com/thoughtworks/dsl/keywords/Typed.scala @@ -2,7 +2,7 @@ package com.thoughtworks.dsl package keywords /** A type annotated keyword */ -opaque type Typed[Keyword, Value] = Keyword +opaque type Typed[Keyword, Value] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[Keyword] object Typed { given [Keyword, Value] @@ -12,6 +12,6 @@ object Typed { ): Dsl.PolyCont[Typed[Keyword, Value], Domain, Value] = dsl - @inline def apply[Keyword, Value]: Keyword =:= Typed[Keyword, Value] = summon + @inline def apply[Keyword, Value]: Keyword =:= Typed[Keyword, Value] = Dsl.OpaqueKeyword.Of.apply } diff --git a/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala b/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala index 6551ff1b7..c307e71fb 100644 --- a/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala +++ b/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala @@ -21,7 +21,7 @@ import scala.util.control.NonFatal * @see * [[dsl]] for usage of this [[Using]] keyword in continuations */ -opaque type Using[R <: AutoCloseable] = R +opaque type Using[R <: AutoCloseable] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[R] object Using { given [R <: AutoCloseable]: AsKeyword.IsKeyword[Using[R], R] with {} @@ -67,7 +67,7 @@ object Using { */ def scopeExit(r: ScopeExitHandler) = r - def apply[R <: AutoCloseable]: R =:= Using[R] = summon + def apply[R <: AutoCloseable]: R =:= Using[R] = Dsl.OpaqueKeyword.Of.apply given [ R <: AutoCloseable, diff --git a/keywords-While/src/main/scala/com/thoughtworks/dsl/keywords/While.scala b/keywords-While/src/main/scala/com/thoughtworks/dsl/keywords/While.scala index 4f721bf8f..25d5e84d8 100644 --- a/keywords-While/src/main/scala/com/thoughtworks/dsl/keywords/While.scala +++ b/keywords-While/src/main/scala/com/thoughtworks/dsl/keywords/While.scala @@ -9,7 +9,7 @@ case class While[ ]( condition: ConditionKeyword, body: BodyKeyword -) +) extends Dsl.Keyword object While { given [ ConditionKeyword, diff --git a/keywords-WithFilter/src/main/scala/com/thoughtworks/dsl/keywords/WithFilter.scala b/keywords-WithFilter/src/main/scala/com/thoughtworks/dsl/keywords/WithFilter.scala index 9b2c8bb2d..bdb873fde 100644 --- a/keywords-WithFilter/src/main/scala/com/thoughtworks/dsl/keywords/WithFilter.scala +++ b/keywords-WithFilter/src/main/scala/com/thoughtworks/dsl/keywords/WithFilter.scala @@ -4,7 +4,7 @@ import com.thoughtworks.dsl.Dsl final case class WithFilter[UpstreamKeyword, UpstreamValue]( upstream: UpstreamKeyword, condition: UpstreamValue => Boolean -) +) extends Dsl.Keyword object WithFilter { given [UpstreamKeyword, UpstreamValue]: Dsl.AsKeyword.IsKeyword[WithFilter[ diff --git a/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala b/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala index d4ba3cd4b..46451dd08 100644 --- a/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala +++ b/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala @@ -39,7 +39,7 @@ import scala.language.higherKinds * }}} * @see [[comprehension]] if you want to use traditional `for` comprehension instead of !-notation. */ -opaque type Yield[Element] = Element +opaque type Yield[Element] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[Element] private[keywords] trait LowPriorityYield3 { @@ -90,16 +90,16 @@ private[keywords] trait LowPriorityYield0 extends LowPriorityYield1 object Yield extends LowPriorityYield0 { given [Element]: AsKeyword.IsKeyword[Yield[Element], Unit] with {} - def apply[Element]: Element =:= Yield[Element] = summon + def apply[Element]: Element =:= Yield[Element] = Dsl.OpaqueKeyword.Of.apply def apply[A](element0: A, element1: A, elements: A*) = { From(element0 +: element1 +: elements) } - opaque type From[FromCollection <: TraversableOnce[_]] = FromCollection + opaque type From[FromCollection <: TraversableOnce[_]] <: Dsl.OpaqueKeyword = Dsl.OpaqueKeyword.Of[FromCollection] object From { given [FromCollection <: TraversableOnce[_]]: AsKeyword.IsKeyword[From[FromCollection], Unit] with {} - def apply[FromCollection <: TraversableOnce[_]]: FromCollection =:= From[FromCollection] = summon + def apply[FromCollection <: TraversableOnce[_]]: FromCollection =:= From[FromCollection] = Dsl.OpaqueKeyword.Of.apply } @@ -180,7 +180,7 @@ object Yield extends LowPriorityYield0 { } } - given implicitYieldFrom[FromCollection <: TraversableOnce[_]]: AsKeyword.IsKeywordSubtype[FromCollection, From[FromCollection], Unit] with {} + given implicitYieldFrom[FromCollection <: TraversableOnce[_]]: AsKeyword[FromCollection, From[FromCollection], Unit] = From(_) implicit def streamYieldFromDsl[A, FromCollection <: Iterable[A]]: Dsl[From[FromCollection], Stream[A], Unit] = new Dsl[From[FromCollection], Stream[A], Unit] { @@ -189,7 +189,7 @@ object Yield extends LowPriorityYield0 { } } - given implicitYield[Element]: AsKeyword.IsKeywordSubtype[Element, Yield[Element], Unit] with {} + given implicitYield[Element]: AsKeyword[Element, Yield[Element], Unit] = Yield(_) implicit def streamYieldDsl[Element, That >: Element]: Dsl[Yield[Element], Stream[That], Unit] = new Dsl[Yield[Element], Stream[That], Unit] { From 504b83b979a3f8859e31417d53e1f22ef085dc19 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Sun, 12 Dec 2021 00:22:24 -0800 Subject: [PATCH 4/6] Move extension methods to bangnotation.Ops --- .../com/thoughtworks/dsl/bangnotation.scala | 42 ++++++++++--------- .../dsl/keywords/ReturnSpec.scala | 2 +- .../dsl/domains/Continuation.scala | 2 +- .../thoughtworks/dsl/domains/RaiiSpec.scala | 2 +- .../thoughtworks/dsl/domains/UsingSpec.scala | 2 +- .../thoughtworks/dsl/domains/taskSpec.scala | 2 +- .../com/thoughtworks/dsl/domains/Task.scala | 6 +-- .../com/thoughtworks/dsl/domains/scalaz.scala | 2 +- .../thoughtworks/dsl/domains/scalazSpec.scala | 2 +- .../dsl/keywords/AsynchronousIo.scala | 6 +-- .../com/thoughtworks/dsl/keywords/Await.scala | 6 +-- .../thoughtworks/dsl/keywords/AwaitTest.scala | 2 +- .../thoughtworks/dsl/keywords/Continue.scala | 2 +- .../com/thoughtworks/dsl/keywords/Each.scala | 4 +- .../thoughtworks/dsl/keywords/EachSpec.scala | 2 +- .../dsl/keywords/ForEachSpec.scala | 2 +- .../com/thoughtworks/dsl/keywords/Fork.scala | 2 +- .../com/thoughtworks/dsl/keywords/Put.scala | 4 +- .../com/thoughtworks/dsl/keywords/Using.scala | 4 +- .../com/thoughtworks/dsl/keywords/Yield.scala | 4 +- .../thoughtworks/dsl/keywords/YieldSpec.scala | 2 +- .../dsl/keywords/YieldSpec213.scala | 2 +- .../src/main/scala/scala/async/Async.scala | 2 +- 23 files changed, 54 insertions(+), 52 deletions(-) diff --git a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala index 3c7e249f1..860c3da4d 100644 --- a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala +++ b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala @@ -715,25 +715,27 @@ object bangnotation { @annotation.compileTimeOnly("""This method must be called only inside a `reset` or `*` code block.""") def shift[Keyword, Value](keyword: Keyword): Value = ??? - extension [From, Keyword, Value](inline from: From)(using inline asKeyword: Dsl.AsKeyword.SearchIsKeywordFirst[From, Keyword, Value]) - transparent inline def unary_! : Value = { - shift[Keyword, Value](asKeyword(from)) - } - - inline def map[MappedValue]( - mapper: Value => MappedValue - ): FlatMap[Keyword, Value, Pure[MappedValue]] = - FlatMap(asKeyword(from), Pure.apply.liftCo(mapper)) + class Ops: + extension [From, Keyword, Value](inline from: From)(using inline asKeyword: Dsl.AsKeyword.SearchIsKeywordFirst[From, Keyword, Value]) + transparent inline def unary_! : Value = { + shift[Keyword, Value](asKeyword(from)) + } - inline def flatMap[Mapped, MappedValue]( - flatMapper: Value => Mapped - )( - using /*erased*/ Dsl.AsKeyword.IsKeyword[Mapped, MappedValue] - ): FlatMap[Keyword, Value, Mapped] = - FlatMap(asKeyword(from), flatMapper) - - inline def withFilter[Mapped, MappedValue]( - filter: Value => Boolean - ) = - WithFilter(asKeyword(from), filter) + inline def map[MappedValue]( + mapper: Value => MappedValue + ): FlatMap[Keyword, Value, Pure[MappedValue]] = + FlatMap(asKeyword(from), Pure.apply.liftCo(mapper)) + + inline def flatMap[Mapped, MappedValue]( + flatMapper: Value => Mapped + )( + using /*erased*/ Dsl.AsKeyword.IsKeyword[Mapped, MappedValue] + ): FlatMap[Keyword, Value, Mapped] = + FlatMap(asKeyword(from), flatMapper) + + inline def withFilter[Mapped, MappedValue]( + filter: Value => Boolean + ) = + WithFilter(asKeyword(from), filter) + object Ops extends Ops } diff --git a/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala b/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala index 7043b7c02..5add2b738 100644 --- a/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala +++ b/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package keywords import com.thoughtworks.dsl.Dsl.!! -import bangnotation.{_, given} +import bangnotation._, Ops._ import utest.{TestSuite, Tests, given} import Dsl.Run import scala.language.implicitConversions diff --git a/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala b/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala index 5093611bf..e80cb8742 100644 --- a/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala +++ b/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package domains -import bangnotation._ +import bangnotation._, Ops._ import scala.util._ import scala.util.control.NonFatal diff --git a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala index 235c1e168..79d5739a5 100644 --- a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala +++ b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala @@ -3,7 +3,7 @@ package domains import com.thoughtworks.dsl.keywords.{Shift, Yield} import com.thoughtworks.dsl.domains._, Continuation.!! -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import scala.util.control.NonFatal import org.scalatest.freespec.AnyFreeSpec diff --git a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala index 74ca5329f..b1e0c95a2 100644 --- a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala +++ b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala @@ -2,7 +2,7 @@ package com.thoughtworks.dsl package domains import Continuation.!! -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.keywords.{Using, Yield} import org.scalatest.Assertion import org.scalatest.freespec.AnyFreeSpec diff --git a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala index 47aa73704..b8e331232 100644 --- a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala +++ b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package domains -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl.{!!} import org.scalatest.Assertion import scala.language.implicitConversions diff --git a/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala b/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala index 5f6d3d4c8..81cd006fc 100644 --- a/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala +++ b/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala @@ -3,7 +3,7 @@ package domains import com.thoughtworks.dsl.domains.Continuation, Continuation.!! import com.thoughtworks.dsl.keywords.Shift -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import scala.collection._ import scala.collection.generic.CanBuildFrom @@ -25,7 +25,7 @@ import scala.util.control.TailCalls.TailRec * * {{{ * import com.thoughtworks.dsl.Dsl._ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * import com.thoughtworks.dsl._ * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ @@ -87,7 +87,7 @@ object Task extends TaskPlatformSpecificFunctions { * * @example All the code after a `!switchExecutionContext` should be executed on `executionContext` * {{{ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * import com.thoughtworks.dsl.domains.Task * import org.scalatest.Assertion * import scala.concurrent.ExecutionContext diff --git a/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala b/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala index 86462f0f4..33f70829d 100644 --- a/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala +++ b/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala @@ -27,7 +27,7 @@ import scala.util.control.NonFatal * import com.thoughtworks.dsl.keywords.Monadic * import com.thoughtworks.dsl.keywords.Monadic.implicitMonadic * import com.thoughtworks.dsl.domains.scalaz.given - * import com.thoughtworks.dsl.bangnotation.{unary_!, reset} + * import com.thoughtworks.dsl.bangnotation._, Ops.unary_! * * val trampoline3 = Trampoline.done(3) * diff --git a/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala b/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala index d63e8d3bf..97030db7b 100644 --- a/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala +++ b/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl.domains -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl.!! import _root_.scalaz.\/ import _root_.scalaz.\/- diff --git a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala index 0c53d3146..7628f26ee 100644 --- a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala +++ b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala @@ -15,7 +15,7 @@ import scala.util.control.NonFatal * * {{{ * import java.nio._, file._, channels._ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * import com.thoughtworks.dsl.domains.Task * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ @@ -44,11 +44,11 @@ import scala.util.control.NonFatal * It asynchronously iterates elements `Seq`, `ArrayBuffer` and `String` with the help of [[keywords.Each]], * managed native resources with the help of [[keywords.Using]], * performs previously created `readAll` task with the help of [[keywords.Shift]], - * and finally converts the return type [[comprehension.ComprehensionOps.as as]] a `Task[Vector[Char]]`. + * and finally converts the return type [[bangnotation.Ops.as as]] a `Task[Vector[Char]]`. * * {{{ * import com.thoughtworks.dsl._ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ * import com.thoughtworks.dsl.domains.Task diff --git a/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala b/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala index 9b74fd059..7487189f1 100644 --- a/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala +++ b/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala @@ -16,7 +16,7 @@ import scala.language.implicitConversions * @example * Given a [[scala.concurrent.Future Future]]: * {{{ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * import scala.concurrent.Future * val myFuture40 = Future { * 40 @@ -54,7 +54,7 @@ import scala.language.implicitConversions * `!Await` can be used together with `try` / `catch` / `finally`. * {{{ * import scala.concurrent.Future - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * val buffer = new StringBuffer * def recoverFuture = Future { * buffer.append("Oh") @@ -81,7 +81,7 @@ import scala.language.implicitConversions * {{{ * import scala.concurrent.Future * import com.thoughtworks.dsl.keywords.{Get, Return} - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * val buffer = new StringBuffer * def recoverFuture = Future { * buffer.append("Oh") diff --git a/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala b/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala index bbcc6444c..33a884446 100644 --- a/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala +++ b/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl -import bangnotation._ +import bangnotation._, Ops._ import Dsl._ import keywords._, Match._ import concurrent.ExecutionContext.Implicits.global diff --git a/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala b/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala index 620cdbd29..eb90b4f31 100644 --- a/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala +++ b/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala @@ -22,7 +22,7 @@ sealed class Continue extends Dsl.Keyword * @example [[Each]] and [[Continue$ Continue]] can be used to calculate composite numbers and prime numbers. * * {{{ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * def compositeNumbersBelow(maxNumber: Int) = *[collection.immutable.HashSet] { * val factor = !Each(2 until math.ceil(math.sqrt(maxNumber)).toInt) * !Each(2 * factor until maxNumber by factor) diff --git a/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala b/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala index 578a1fe52..83f28239e 100644 --- a/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala +++ b/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl.keywords -import com.thoughtworks.dsl.bangnotation.{reset, unary_!} +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl import com.thoughtworks.dsl.Dsl.{!!, AsKeyword} @@ -15,7 +15,7 @@ import scala.collection.mutable.Builder * @example [[Each]] keywords can be used to calculate cartesian product. * * {{{ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * def cartesianProduct = reset (List(!Each(Array(1, 2, 3)) * !Each(Vector(1, 10, 100, 1000)))) * cartesianProduct should be(List(1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000)) * }}} diff --git a/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala b/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala index 1f4b54b2a..81344c587 100644 --- a/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala +++ b/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl.keywords import com.thoughtworks.dsl.Dsl.!! -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers diff --git a/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala b/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala index caf07adf2..8e4f4e673 100644 --- a/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala +++ b/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala @@ -2,7 +2,7 @@ package com.thoughtworks.dsl.keywords import com.thoughtworks.dsl.Dsl.!! import org.scalatest.freespec.AnyFreeSpec -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import org.scalatest.matchers.should.Matchers /** @author 杨博 (Yang Bo) diff --git a/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala b/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala index f0680d578..15c07319e 100644 --- a/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala +++ b/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala @@ -3,7 +3,7 @@ package com.thoughtworks.dsl.keywords import java.io.{PrintStream, PrintWriter} import java.util.concurrent.atomic.AtomicInteger -import com.thoughtworks.dsl.bangnotation.{`*`, reify, unary_!} +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl import com.thoughtworks.dsl.Dsl.{!!, AsKeyword} diff --git a/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala b/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala index 7fa74fe62..cb9eea2f3 100644 --- a/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala +++ b/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala @@ -17,7 +17,7 @@ import com.thoughtworks.dsl.Dsl.AsKeyword * character of the parameter. * * {{{ - * import com.thoughtworks.dsl.bangnotation.{unary_!, reset} + * import com.thoughtworks.dsl.bangnotation._, Ops._ * def upperCasedLastCharacter = reset[String => Char] { * val initialValue = !Get[String]() * !Put(initialValue.toUpperCase) @@ -39,7 +39,7 @@ import com.thoughtworks.dsl.Dsl.AsKeyword * The following code creates a formatter that [[Put]] parts of content into a `Vector[Any]` of string buffers. * * {{{ - * import com.thoughtworks.dsl.bangnotation.{reset, unary_!} + * import com.thoughtworks.dsl.bangnotation._, Ops._ * def formatter = reset[Double => Int => Vector[Any] => String] { * !Put(!Get[Vector[Any]]() :+ "x=") * !Put(!Get[Vector[Any]]() :+ !Get[Double]()) diff --git a/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala b/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala index c307e71fb..3517e0664 100644 --- a/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala +++ b/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package keywords -import com.thoughtworks.dsl.bangnotation.{`*`, reify, reset, unary_!} +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl import com.thoughtworks.dsl.Dsl.!! import com.thoughtworks.dsl.Dsl.AsKeyword @@ -47,7 +47,7 @@ object Using { * {{{ * import scala.concurrent.Future * import com.thoughtworks.dsl.keywords.Using.scopeExit - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * var n = 1 * def multiplicationAfterAddition = *[Future] { * !scopeExit { () => diff --git a/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala b/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala index 46451dd08..47f258cbe 100644 --- a/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala +++ b/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala @@ -18,13 +18,13 @@ import scala.language.higherKinds * or it will not compile. * * {{{ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * "def f(): Int = !Yield(1)" shouldNot compile * }}} * * @example [[Yield]] keywords can be used together with other keywords. * {{{ - * import com.thoughtworks.dsl.bangnotation._ + * import com.thoughtworks.dsl.bangnotation._, Ops._ * def gccFlagBuilder(sourceFile: String, includes: String*) = reset[Stream[String]] { * !Yield("gcc") * !Yield("-c") diff --git a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala index b2f0ff15a..49f80b5ad 100644 --- a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala +++ b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl package keywords -import bangnotation._ +import bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl.!! import scala.annotation.tailrec import scala.collection.{LinearSeq, SeqView} diff --git a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala index 1b6614775..83a71da81 100644 --- a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala +++ b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl.keywords -import com.thoughtworks.dsl.bangnotation._ +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.Dsl.!! import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers diff --git a/scala-async/src/main/scala/scala/async/Async.scala b/scala-async/src/main/scala/scala/async/Async.scala index e12ffde8b..d82226f80 100644 --- a/scala-async/src/main/scala/scala/async/Async.scala +++ b/scala-async/src/main/scala/scala/async/Async.scala @@ -1,6 +1,6 @@ package scala.async import scala.concurrent.Future -import com.thoughtworks.dsl.bangnotation.{`*`, unary_!} +import com.thoughtworks.dsl.bangnotation._, Ops._ import com.thoughtworks.dsl.keywords.Await /** Async blocks provide a direct means to work with From ecf1e142a5f84dd1ea4f85bd016318d343e71e17 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Sun, 12 Dec 2021 01:06:24 -0800 Subject: [PATCH 5/6] Move bangnoation.Ops to Dsl --- Dsl/build.sbt | 2 ++ .../main/scala/com/thoughtworks/dsl/Dsl.scala | 19 ++++++++++++++ .../com/thoughtworks/dsl/bangnotation.scala | 26 +++---------------- .../dsl/keywords/ReturnSpec.scala | 2 +- .../dsl/domains/Continuation.scala | 2 +- .../thoughtworks/dsl/domains/RaiiSpec.scala | 2 +- .../thoughtworks/dsl/domains/UsingSpec.scala | 2 +- .../thoughtworks/dsl/domains/taskSpec.scala | 2 +- .../com/thoughtworks/dsl/domains/Task.scala | 6 ++--- .../com/thoughtworks/dsl/domains/scalaz.scala | 2 +- .../thoughtworks/dsl/domains/scalazSpec.scala | 2 +- .../dsl/keywords/AsynchronousIo.scala | 6 ++--- .../com/thoughtworks/dsl/keywords/Await.scala | 6 ++--- .../thoughtworks/dsl/keywords/AwaitTest.scala | 2 +- .../thoughtworks/dsl/keywords/Continue.scala | 2 +- .../com/thoughtworks/dsl/keywords/Each.scala | 4 +-- .../thoughtworks/dsl/keywords/EachSpec.scala | 2 +- .../dsl/keywords/ForEachSpec.scala | 2 +- .../com/thoughtworks/dsl/keywords/Fork.scala | 2 +- .../com/thoughtworks/dsl/keywords/Put.scala | 4 +-- .../com/thoughtworks/dsl/keywords/Using.scala | 4 +-- .../com/thoughtworks/dsl/keywords/Yield.scala | 4 +-- .../thoughtworks/dsl/keywords/YieldSpec.scala | 2 +- .../dsl/keywords/YieldSpec213.scala | 2 +- .../src/main/scala/scala/async/Async.scala | 2 +- 25 files changed, 56 insertions(+), 55 deletions(-) diff --git a/Dsl/build.sbt b/Dsl/build.sbt index 6032d2b0b..66edd5c83 100644 --- a/Dsl/build.sbt +++ b/Dsl/build.sbt @@ -1,3 +1,5 @@ libraryDependencies += "com.github.sbt" % "junit-interface" % "0.13.2" % Test libraryDependencies += "junit" % "junit" % "4.13.2" % Test + +libraryDependencies += "com.thoughtworks.dsl" %%% "bangnotation" % "2.0.0-M2+100-66bf7c43" \ No newline at end of file diff --git a/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala b/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala index 44b6d88a6..d0f6d059b 100644 --- a/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala +++ b/Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala @@ -529,4 +529,23 @@ object Dsl extends LowPriorityDsl0 { dsl.cpsApply(keyword, handler) } + extension [From, Keyword, Value](inline from: From)(using inline asKeyword: Dsl.AsKeyword.SearchIsKeywordFirst[From, Keyword, Value]) + + inline def map[MappedValue]( + mapper: Value => MappedValue + ): keywords.FlatMap[Keyword, Value, keywords.Pure[MappedValue]] = + keywords.FlatMap(asKeyword(from), keywords.Pure.apply.liftCo(mapper)) + + inline def flatMap[Mapped, MappedValue]( + flatMapper: Value => Mapped + )( + using /*erased*/ AsKeyword.IsKeyword[Mapped, MappedValue] + ): keywords.FlatMap[Keyword, Value, Mapped] = + keywords.FlatMap(asKeyword(from), flatMapper) + + inline def withFilter[Mapped, MappedValue]( + filter: Value => Boolean + ) = + keywords.WithFilter(asKeyword(from), filter) + } diff --git a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala index 860c3da4d..3ab7bba3c 100644 --- a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala +++ b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala @@ -715,27 +715,9 @@ object bangnotation { @annotation.compileTimeOnly("""This method must be called only inside a `reset` or `*` code block.""") def shift[Keyword, Value](keyword: Keyword): Value = ??? - class Ops: - extension [From, Keyword, Value](inline from: From)(using inline asKeyword: Dsl.AsKeyword.SearchIsKeywordFirst[From, Keyword, Value]) - transparent inline def unary_! : Value = { - shift[Keyword, Value](asKeyword(from)) - } + extension [From, Keyword, Value](inline from: From)(using inline asKeyword: Dsl.AsKeyword.SearchIsKeywordFirst[From, Keyword, Value]) + transparent inline def unary_! : Value = { + shift[Keyword, Value](asKeyword(from)) + } - inline def map[MappedValue]( - mapper: Value => MappedValue - ): FlatMap[Keyword, Value, Pure[MappedValue]] = - FlatMap(asKeyword(from), Pure.apply.liftCo(mapper)) - - inline def flatMap[Mapped, MappedValue]( - flatMapper: Value => Mapped - )( - using /*erased*/ Dsl.AsKeyword.IsKeyword[Mapped, MappedValue] - ): FlatMap[Keyword, Value, Mapped] = - FlatMap(asKeyword(from), flatMapper) - - inline def withFilter[Mapped, MappedValue]( - filter: Value => Boolean - ) = - WithFilter(asKeyword(from), filter) - object Ops extends Ops } diff --git a/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala b/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala index 5add2b738..1fde17953 100644 --- a/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala +++ b/bangnotation/src/test/scala/com/thoughtworks/dsl/keywords/ReturnSpec.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package keywords import com.thoughtworks.dsl.Dsl.!! -import bangnotation._, Ops._ +import bangnotation._ import utest.{TestSuite, Tests, given} import Dsl.Run import scala.language.implicitConversions diff --git a/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala b/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala index e80cb8742..5093611bf 100644 --- a/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala +++ b/domains-Continuation/src/main/scala/com/thoughtworks/dsl/domains/Continuation.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package domains -import bangnotation._, Ops._ +import bangnotation._ import scala.util._ import scala.util.control.NonFatal diff --git a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala index 79d5739a5..235c1e168 100644 --- a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala +++ b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/RaiiSpec.scala @@ -3,7 +3,7 @@ package domains import com.thoughtworks.dsl.keywords.{Shift, Yield} import com.thoughtworks.dsl.domains._, Continuation.!! -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import scala.util.control.NonFatal import org.scalatest.freespec.AnyFreeSpec diff --git a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala index b1e0c95a2..74ca5329f 100644 --- a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala +++ b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/UsingSpec.scala @@ -2,7 +2,7 @@ package com.thoughtworks.dsl package domains import Continuation.!! -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.keywords.{Using, Yield} import org.scalatest.Assertion import org.scalatest.freespec.AnyFreeSpec diff --git a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala index b8e331232..47aa73704 100644 --- a/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala +++ b/domains-Task/.jvm/src/test/scala/com/thoughtworks/dsl/domains/taskSpec.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package domains -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.Dsl.{!!} import org.scalatest.Assertion import scala.language.implicitConversions diff --git a/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala b/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala index 81cd006fc..5f6d3d4c8 100644 --- a/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala +++ b/domains-Task/src/main/scala/com/thoughtworks/dsl/domains/Task.scala @@ -3,7 +3,7 @@ package domains import com.thoughtworks.dsl.domains.Continuation, Continuation.!! import com.thoughtworks.dsl.keywords.Shift -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import scala.collection._ import scala.collection.generic.CanBuildFrom @@ -25,7 +25,7 @@ import scala.util.control.TailCalls.TailRec * * {{{ * import com.thoughtworks.dsl.Dsl._ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * import com.thoughtworks.dsl._ * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ @@ -87,7 +87,7 @@ object Task extends TaskPlatformSpecificFunctions { * * @example All the code after a `!switchExecutionContext` should be executed on `executionContext` * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * import com.thoughtworks.dsl.domains.Task * import org.scalatest.Assertion * import scala.concurrent.ExecutionContext diff --git a/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala b/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala index 33f70829d..b28553784 100644 --- a/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala +++ b/domains-scalaz/src/main/scala/com/thoughtworks/dsl/domains/scalaz.scala @@ -27,7 +27,7 @@ import scala.util.control.NonFatal * import com.thoughtworks.dsl.keywords.Monadic * import com.thoughtworks.dsl.keywords.Monadic.implicitMonadic * import com.thoughtworks.dsl.domains.scalaz.given - * import com.thoughtworks.dsl.bangnotation._, Ops.unary_! + * import com.thoughtworks.dsl.bangnotation._ * * val trampoline3 = Trampoline.done(3) * diff --git a/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala b/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala index 97030db7b..d63e8d3bf 100644 --- a/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala +++ b/domains-scalaz/src/test/scala/com/thoughtworks/dsl/domains/scalazSpec.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl.domains -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.Dsl.!! import _root_.scalaz.\/ import _root_.scalaz.\/- diff --git a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala index 7628f26ee..0f41a75f1 100644 --- a/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala +++ b/keywords-AsynchronousIo/src/main/scala/com/thoughtworks/dsl/keywords/AsynchronousIo.scala @@ -15,14 +15,14 @@ import scala.util.control.NonFatal * * {{{ * import java.nio._, file._, channels._ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * import com.thoughtworks.dsl.domains.Task * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ * import com.thoughtworks.dsl.keywords.AsynchronousIo.ReadFile * import scala.collection.mutable.ArrayBuffer * import scala.io.Codec - * def readAll(channel: AsynchronousFileChannel, temporaryBufferSize: Int = 4096): Task[ArrayBuffer[CharBuffer]] = *[Task] { + * def readAll(channel: AsynchronousFileChannel, temporaryBufferSize: Int = 4096): Task[ArrayBuffer[CharBuffer]] = Task { * val charBuffers = ArrayBuffer.empty[CharBuffer] * val decoder = Codec.UTF8.decoder * val byteBuffer = ByteBuffer.allocate(temporaryBufferSize) @@ -36,7 +36,6 @@ import scala.util.control.NonFatal * charBuffers * } * }}} - * * `Task`s created from !-notation can be used in `for`-comprehension, * and other keywords can be used together in the same `for` block. * @@ -48,7 +47,6 @@ import scala.util.control.NonFatal * * {{{ * import com.thoughtworks.dsl._ - * import com.thoughtworks.dsl.bangnotation._, Ops._ * import com.thoughtworks.dsl.keywords._ * import com.thoughtworks.dsl.keywords.Shift._ * import com.thoughtworks.dsl.domains.Task diff --git a/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala b/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala index 7487189f1..9b74fd059 100644 --- a/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala +++ b/keywords-Await/src/main/scala/com/thoughtworks/dsl/keywords/Await.scala @@ -16,7 +16,7 @@ import scala.language.implicitConversions * @example * Given a [[scala.concurrent.Future Future]]: * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * import scala.concurrent.Future * val myFuture40 = Future { * 40 @@ -54,7 +54,7 @@ import scala.language.implicitConversions * `!Await` can be used together with `try` / `catch` / `finally`. * {{{ * import scala.concurrent.Future - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * val buffer = new StringBuffer * def recoverFuture = Future { * buffer.append("Oh") @@ -81,7 +81,7 @@ import scala.language.implicitConversions * {{{ * import scala.concurrent.Future * import com.thoughtworks.dsl.keywords.{Get, Return} - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * val buffer = new StringBuffer * def recoverFuture = Future { * buffer.append("Oh") diff --git a/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala b/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala index 33a884446..bbcc6444c 100644 --- a/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala +++ b/keywords-Await/src/test/scala/com/thoughtworks/dsl/keywords/AwaitTest.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl -import bangnotation._, Ops._ +import bangnotation._ import Dsl._ import keywords._, Match._ import concurrent.ExecutionContext.Implicits.global diff --git a/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala b/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala index eb90b4f31..620cdbd29 100644 --- a/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala +++ b/keywords-Continue/src/main/scala/com/thoughtworks/dsl/keywords/Continue.scala @@ -22,7 +22,7 @@ sealed class Continue extends Dsl.Keyword * @example [[Each]] and [[Continue$ Continue]] can be used to calculate composite numbers and prime numbers. * * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * def compositeNumbersBelow(maxNumber: Int) = *[collection.immutable.HashSet] { * val factor = !Each(2 until math.ceil(math.sqrt(maxNumber)).toInt) * !Each(2 * factor until maxNumber by factor) diff --git a/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala b/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala index 83f28239e..7c35769b1 100644 --- a/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala +++ b/keywords-Each/src/main/scala/com/thoughtworks/dsl/keywords/Each.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl.keywords -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.Dsl import com.thoughtworks.dsl.Dsl.{!!, AsKeyword} @@ -15,7 +15,7 @@ import scala.collection.mutable.Builder * @example [[Each]] keywords can be used to calculate cartesian product. * * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * def cartesianProduct = reset (List(!Each(Array(1, 2, 3)) * !Each(Vector(1, 10, 100, 1000)))) * cartesianProduct should be(List(1, 10, 100, 1000, 2, 20, 200, 2000, 3, 30, 300, 3000)) * }}} diff --git a/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala b/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala index 81344c587..1f4b54b2a 100644 --- a/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala +++ b/keywords-Each/src/test/scala/com/thoughtworks/dsl/keywords/EachSpec.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl.keywords import com.thoughtworks.dsl.Dsl.!! -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers diff --git a/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala b/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala index 8e4f4e673..caf07adf2 100644 --- a/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala +++ b/keywords-ForEach/src/test/scala/com/thoughtworks/dsl/keywords/ForEachSpec.scala @@ -2,7 +2,7 @@ package com.thoughtworks.dsl.keywords import com.thoughtworks.dsl.Dsl.!! import org.scalatest.freespec.AnyFreeSpec -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import org.scalatest.matchers.should.Matchers /** @author 杨博 (Yang Bo) diff --git a/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala b/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala index 15c07319e..306fe2f66 100644 --- a/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala +++ b/keywords-Fork/src/main/scala/com/thoughtworks/dsl/keywords/Fork.scala @@ -3,7 +3,7 @@ package com.thoughtworks.dsl.keywords import java.io.{PrintStream, PrintWriter} import java.util.concurrent.atomic.AtomicInteger -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.Dsl import com.thoughtworks.dsl.Dsl.{!!, AsKeyword} diff --git a/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala b/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala index cb9eea2f3..efceac098 100644 --- a/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala +++ b/keywords-Put/src/main/scala/com/thoughtworks/dsl/keywords/Put.scala @@ -17,7 +17,7 @@ import com.thoughtworks.dsl.Dsl.AsKeyword * character of the parameter. * * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * def upperCasedLastCharacter = reset[String => Char] { * val initialValue = !Get[String]() * !Put(initialValue.toUpperCase) @@ -39,7 +39,7 @@ import com.thoughtworks.dsl.Dsl.AsKeyword * The following code creates a formatter that [[Put]] parts of content into a `Vector[Any]` of string buffers. * * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * def formatter = reset[Double => Int => Vector[Any] => String] { * !Put(!Get[Vector[Any]]() :+ "x=") * !Put(!Get[Vector[Any]]() :+ !Get[Double]()) diff --git a/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala b/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala index 3517e0664..21ccf700a 100644 --- a/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala +++ b/keywords-Using/src/main/scala/com/thoughtworks/dsl/keywords/Using.scala @@ -1,7 +1,7 @@ package com.thoughtworks.dsl package keywords -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.Dsl import com.thoughtworks.dsl.Dsl.!! import com.thoughtworks.dsl.Dsl.AsKeyword @@ -47,7 +47,7 @@ object Using { * {{{ * import scala.concurrent.Future * import com.thoughtworks.dsl.keywords.Using.scopeExit - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * var n = 1 * def multiplicationAfterAddition = *[Future] { * !scopeExit { () => diff --git a/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala b/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala index 47f258cbe..46451dd08 100644 --- a/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala +++ b/keywords-Yield/src/main/scala/com/thoughtworks/dsl/keywords/Yield.scala @@ -18,13 +18,13 @@ import scala.language.higherKinds * or it will not compile. * * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * "def f(): Int = !Yield(1)" shouldNot compile * }}} * * @example [[Yield]] keywords can be used together with other keywords. * {{{ - * import com.thoughtworks.dsl.bangnotation._, Ops._ + * import com.thoughtworks.dsl.bangnotation._ * def gccFlagBuilder(sourceFile: String, includes: String*) = reset[Stream[String]] { * !Yield("gcc") * !Yield("-c") diff --git a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala index 49f80b5ad..b2f0ff15a 100644 --- a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala +++ b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl package keywords -import bangnotation._, Ops._ +import bangnotation._ import com.thoughtworks.dsl.Dsl.!! import scala.annotation.tailrec import scala.collection.{LinearSeq, SeqView} diff --git a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala index 83a71da81..1b6614775 100644 --- a/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala +++ b/keywords-Yield/src/test/scala/com/thoughtworks/dsl/keywords/YieldSpec213.scala @@ -1,6 +1,6 @@ package com.thoughtworks.dsl.keywords -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.Dsl.!! import org.scalatest.freespec.AnyFreeSpec import org.scalatest.matchers.should.Matchers diff --git a/scala-async/src/main/scala/scala/async/Async.scala b/scala-async/src/main/scala/scala/async/Async.scala index d82226f80..1922e6e1f 100644 --- a/scala-async/src/main/scala/scala/async/Async.scala +++ b/scala-async/src/main/scala/scala/async/Async.scala @@ -1,6 +1,6 @@ package scala.async import scala.concurrent.Future -import com.thoughtworks.dsl.bangnotation._, Ops._ +import com.thoughtworks.dsl.bangnotation._ import com.thoughtworks.dsl.keywords.Await /** Async blocks provide a direct means to work with From 3f77d72e5123a6246d874b3bb6818b9319347360 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Sun, 12 Dec 2021 01:19:59 -0800 Subject: [PATCH 6/6] Add reifyFunction --- .../com/thoughtworks/dsl/bangnotation.scala | 67 ++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) diff --git a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala index 3ab7bba3c..c98b24bd7 100644 --- a/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala +++ b/bangnotation/src/main/scala/com/thoughtworks/dsl/bangnotation.scala @@ -67,10 +67,31 @@ object bangnotation { } } - def resetDefDef(defDef: DefDef): DefDef = { + def reifyFunction[R, A](function: quoted.Expr[R => A])(using quoted.Type[R], quoted.Type[A]): quoted.Expr[R => Any] = { + val functionTerm = function.asTerm.underlyingArgument + functionTerm match { + case block @ qctx.reflect.Block( + List( + defDef: DefDef + ), + closure @ Closure(ident: Ident, _) + ) if (ident.name == defDef.name) => + qctx.reflect.Block.copy(block)( + List(reifyDefDef(defDef)), + closure, + ).usingExpr { [F] => (f: quoted.Expr[F]) => (tf: quoted.Type[F]) => + f.asInstanceOf[quoted.Expr[R => Any]] + } + case _ => + report.error("Expect a function literal", functionTerm.underlyingArgument.pos) + '{???} + } + } + + def reifyDefDef(defDef: DefDef): DefDef = { val DefDef(name, typeParamsAndParams, tpt, rhsOption) = defDef rhsOption match { - case Some(rhs) if resetDescendant => + case Some(rhs) => rhs match { case matchTree @ qctx.reflect.Match(scrutinee, cases) => DefDef.copy(defDef)( @@ -94,6 +115,40 @@ object bangnotation { } } + def resetDefDef(defDef: DefDef): DefDef = { + val DefDef(name, typeParamsAndParams, tpt, rhsOption) = defDef + rhsOption match { + case Some(rhs) if resetDescendant => + rhs match { + case matchTree @ qctx.reflect.Match(scrutinee, cases) => + DefDef.copy(defDef)( + name, typeParamsAndParams, tpt, Some( + qctx.reflect.Match.copy(matchTree)( + scrutinee, + cases.map { + case caseDef @ CaseDef(pattern, guard, caseRhs) => + CaseDef.copy(caseDef)(pattern, guard, reifyTerm(caseRhs)) + } + ) + ) + ) + case _ => + DefDef.copy(defDef)( + name, typeParamsAndParams, tpt, Some(reifyTerm(rhs)) + ) + } + case _ => + defDef + } + } + + def reifyTerm(term: Term): Term = { + term.usingExpr { [Value] => (body: quoted.Expr[Value]) => (tv: quoted.Type[Value]) => + given quoted.Type[Value] = tv + reify[Value](body).asTerm + } + } + def resetTerm(term: Term): Term = { term.usingExpr { [Value] => (body: quoted.Expr[Value]) => (tv: quoted.Type[Value]) => given quoted.Type[Value] = tv @@ -686,7 +741,9 @@ object bangnotation { def reify[V](body: quoted.Expr[_])(using qctx: Quotes, tv: quoted.Type[V]): quoted.Expr[_] = { Macros[qctx.type](resetDescendant = false).reify[V](body/*.underlyingArgument*/) } - + def reifyFunction[R, A](body: quoted.Expr[R => A])(using qctx: Quotes, tr: quoted.Type[R], ta: quoted.Type[A]): quoted.Expr[R => Any] = { + Macros[qctx.type](resetDescendant = false).reifyFunction[R, A](body/*.underlyingArgument*/) + } def reset[From, To](body: quoted.Expr[From])(using qctx: Quotes, fromType: quoted.Type[From], toType: quoted.Type[To]): quoted.Expr[To] = { import qctx.reflect.{_, given} val result: quoted.Expr[To] = Macros[qctx.type](resetDescendant = false).reset(body/*.underlyingArgument*/) @@ -701,6 +758,10 @@ object bangnotation { Macros.reify[Value]('value) } + transparent inline def reifyFunction[State, Value](inline value: State => Value): State => Any = ${ + Macros.reifyFunction[State, Value]('value) + } + class *[Functor[_]] { inline def apply[Value](inline value: Value): Functor[Value] = ${ Macros.reset[Value, Functor[Value]]('value)