Skip to content
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

Remove unused Shift instance #485

Merged
merged 3 commits into from
Dec 8, 2021
Merged
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
75 changes: 1 addition & 74 deletions Dsl/src/main/scala/com/thoughtworks/dsl/Dsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,80 +123,7 @@ object Dsl extends LowPriorityDsl0 {
)
}

type Continuation[R, +A] = (A => R) => R

// TODO: Move to a separate library ( domains-continuation? )
object Continuation {
@inline
def now[R, A](a: A): R !! A = _(a)

@inline
def empty[R, A](r: R): R !! A = Function.const(r)

@inline
def delay[R, A](a: () => A): R !! A = _(a())

// TODO: turn it into an inline def
// TODO: Think about package object in Scala 3
@inline
def apply[R, A](a: => A): (R !! A) = delay(() => a)

def toTryContinuation[LeftDomain, Value](
task: LeftDomain !! Throwable !! Value
)(handler: Try[Value] => LeftDomain): LeftDomain = {
task { a => failureHandler =>
handler(Success(a))
} { e =>
handler(Failure(e))
}
}

def fromTryContinuation[LeftDomain, Value](
continuation: LeftDomain !! Try[Value]
)(successHandler: Value => LeftDomain !! Throwable)(failureHandler: Throwable => LeftDomain): LeftDomain = {
continuation(
new (Try[Value] => LeftDomain) {
def apply(result: Try[Value]): LeftDomain = {
result match {
case Success(a) =>
val protectedContinuation =
try {
successHandler(a)
} catch {
case NonFatal(e) =>
return failureHandler(e)
}
protectedContinuation(failureHandler)
case Failure(e) =>
failureHandler(e)
}
}
}
)
}

}

type !![R, +A] = Continuation[R, A]
val !! = Continuation

@deprecated("Use bangnotation.reset instead", "Dsl.scala 2.0.0")
private[dsl] /* sealed */ trait ResetAnnotation extends Annotation with StaticAnnotation
@deprecated("Use bangnotation.reset instead", "Dsl.scala 2.0.0")
private[dsl] final class nonTypeConstraintReset extends ResetAnnotation with StaticAnnotation

/** An annotation to explicitly perform reset control operator on a code block.
*
* @note
* This annotation can be automatically added if [[compilerplugins.ResetEverywhere ResetEverywhere]] compiler
* plug-in is enabled.
*/
@deprecated("Use bangnotation.reset instead", "Dsl.scala 2.0.0")
final class reset extends ResetAnnotation with StaticAnnotation with TypeConstraint

/** An annotation to mark a method is a shift control operator. */
@deprecated("Use bangnotation.reset instead", "Dsl.scala 2.0.0")
final class shift extends StaticAnnotation
private[dsl] type !![R, +A] = (A => R) => R

def apply[Keyword, Domain, Value](implicit typeClass: Dsl[Keyword, Domain, Value]): Dsl[Keyword, Domain, Value] =
typeClass
Expand Down
7 changes: 7 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ lazy val bangnotation =
`keywords-While`
)

lazy val `domains-Continuation` =
crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.dependsOn(bangnotation)

lazy val `domains-Task` =
crossProject(JSPlatform, JVMPlatform)
.crossType(CrossType.Pure)
.dependsOn(
`keywords-Shift`,
bangnotation,
`domains-Continuation`,
`keywords-In` % Test,
`keywords-Fork` % Test,
`keywords-Each` % Test,
Expand Down Expand Up @@ -170,6 +176,7 @@ lazy val `keywords-Await` =
.crossType(CrossType.Pure)
.dependsOn(
Dsl,
`domains-Continuation`,
comprehension % Test,
bangnotation % Test,
`domains-Task` % Test,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.thoughtworks.dsl
package domains

import bangnotation._
import scala.util._
import scala.util.control.NonFatal

type Continuation[R, +A] = (A => R) => R

object Continuation {
val !! = this
type !![R, +A] = Continuation[R, A]


@inline
def now[R, A](a: A): R !! A = _(a)

@inline
def empty[R, A](r: R): R !! A = Function.const(r)

@inline
def delay[R, A](a: () => A): R !! A = _(a())

inline def apply[R, A](inline a: A): R !! A = { handler =>
reset(handler(a))
}

def toTryContinuation[LeftDomain, Value](
task: LeftDomain !! Throwable !! Value
)(handler: Try[Value] => LeftDomain): LeftDomain = {
task { a => failureHandler =>
handler(Success(a))
} { e =>
handler(Failure(e))
}
}

def fromTryContinuation[LeftDomain, Value](
continuation: LeftDomain !! Try[Value]
)(successHandler: Value => LeftDomain !! Throwable)(failureHandler: Throwable => LeftDomain): LeftDomain = {
continuation(
new (Try[Value] => LeftDomain) {
def apply(result: Try[Value]): LeftDomain = {
result match {
case Success(a) =>
val protectedContinuation =
try {
successHandler(a)
} catch {
case NonFatal(e) =>
return failureHandler(e)
}
protectedContinuation(failureHandler)
case Failure(e) =>
failureHandler(e)
}
}
}
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import scala.concurrent.SyncVar
import scala.util.Try
import scala.util.control.TailCalls
import scala.concurrent.duration.Duration
import com.thoughtworks.dsl.Dsl.Continuation
import com.thoughtworks.dsl.domains.Continuation
private[domains] trait TaskPlatformSpecificFunctions { this: Task.type =>

def blockingAwait[A](task: Task[A], timeout: Duration = Duration.Inf): A = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.thoughtworks.dsl
package domains

import com.thoughtworks.dsl.Dsl.{!!, Continuation}
import com.thoughtworks.dsl.keywords.{Shift, Yield}
import com.thoughtworks.dsl.domains._
import com.thoughtworks.dsl.domains._, Continuation.!!
import com.thoughtworks.dsl.bangnotation._

import scala.util.control.NonFatal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoughtworks.dsl
package domains

import com.thoughtworks.dsl.Dsl.{!!, Continuation}
import Continuation.!!
import com.thoughtworks.dsl.bangnotation._
import com.thoughtworks.dsl.keywords.{Using, Yield}
import org.scalatest.Assertion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.thoughtworks.dsl
package domains

import com.thoughtworks.dsl.Dsl.{!!, Continuation}
import com.thoughtworks.dsl.domains.Continuation, Continuation.!!
import com.thoughtworks.dsl.keywords.Shift
import com.thoughtworks.dsl.bangnotation._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keywords
import Dsl.IsKeyword
import Dsl.Typed
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.!!
import com.thoughtworks.dsl.domains.Continuation.!!
import scala.concurrent.Await.result
import scala.concurrent.duration.Duration
import scala.concurrent.{ExecutionContext, Future}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Dsl.IsKeyword
import Dsl.Typed

import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.{!!, Continuation}
import com.thoughtworks.dsl.keywords.Shift.{SameDomainStackSafeShiftDsl, StackSafeShiftDsl}

import scala.annotation.tailrec
Expand All @@ -15,7 +14,7 @@ import scala.util.control.TailCalls.TailRec
/** @author
* 杨博 (Yang Bo)
*/
opaque type Shift[R, A] = Dsl.Continuation[R, A]
opaque type Shift[R, A] = (A => R) => R

private[keywords] trait LowPriorityShift1 {

Expand All @@ -30,13 +29,6 @@ private[keywords] trait LowPriorityShift1 {

private[keywords] trait LowPriorityShift0 extends LowPriorityShift1 { this: Shift.type =>

@inline
implicit def stackSafeShiftDsl[Domain, NewDomain, Value](implicit
stackSafeShiftDsl: StackSafeShiftDsl[Domain, NewDomain, Value]
): Dsl[Shift[Domain, Value], NewDomain, Value] = {
stackSafeShiftDsl
}

given [LeftDomain, RightDomain, Value](using
restDsl: SameDomainStackSafeShiftDsl[LeftDomain, RightDomain]
): SameDomainStackSafeShiftDsl[LeftDomain !! RightDomain, Value] = { (keyword, handler) =>
Expand Down Expand Up @@ -151,8 +143,10 @@ object Shift extends LowPriorityShift0 {
taskFlatMap(keyword.continuation, handler)
}

def apply[R, A](continuation: Continuation[R, A]): Shift[R, A] = continuation
private[keywords] type !![R, +A] = (A => R) => R

def apply[R, A](continuation: R !! A): Shift[R, A] = continuation

extension [R, A](shift: Shift[R, A]) def continuation: Continuation[R, A] = shift
extension [R, A](shift: Shift[R, A]) def continuation: R !! A = shift

}