The domain-specific interpreter for Instruction
in Domain
,
which is a dependent type type class that registers an asynchronous callback function,
to handle the Value
inside Instruction
.
The namespace that contains annotations of control operators for delimited continuation.
The namespace that contains annotations of control operators for delimited continuation.
All annotations defined here requires the Scala compiler plugin, which can be added in build.sbt
:
addCompilerPlugin("com.thoughtworks.dsl" %% "compilerplugin" % "latest.release")
Given a class that contains a doShift
method, which is marked as @shift
import com.thoughtworks.dsl.annotations.{reset, shift} implicit class ShiftOps[R, A](f: (A => R) => R) { def cpsApply(handler: A => R): R = f(handler) @shift def doShift: A = sys.error("Calls to this method should have been translated to `cpsApply`") }
When creating a @reset
block that contains the doShift
method,
then the doShift
method should be translated to cpsApply
with a handler of captured rest code in the method,
val explicitResetResult = { val i: Int = { (handler: Int => Int) => handler(4) + 2 }.doShift i * 10 }: @reset explicitResetResult should be { ShiftOps { (handler: Int => Int) => handler(4) + 2 }.cpsApply { i => i * 10 } } explicitResetResult should be(42)
and the result differs if you move the reset annotation.
val explicitResetResult2 = { val i: Int = { (handler: Int => Int) => handler(4) + 2 }.doShift : @reset i * 10 } explicitResetResult2 should be { val i = ShiftOps { (handler: Int => Int) => handler(4) + 2 }.cpsApply(identity) i * 10 } explicitResetResult2 should be(60)
If you omit an explicit reset annotation,
def automaticallyReset: Int = { val i: Int = { (handler: Int => Int) => handler(4) + 2 }.doShift i * 10 }
then the reset operation will be performed on the enclosing method or function automatically.
automaticallyReset should be { ShiftOps { (handler: Int => Int) => handler(4) + 2 }.cpsApply { i => i * 10 } } automaticallyReset should be(42)
Dsl.Instruction for a sophisticated implementation of cpsApply
.
The domain-specific interpreter for
Instruction
inDomain
, which is a dependent type type class that registers an asynchronous callback function, to handle theValue
insideInstruction
.Author:
杨博 (Yang Bo)
The value held inside
Instruction
.