com.thoughtworks.dsl

Type members

Classlikes

@implicitNotFound("The keyword:\n ${Keyword}\nis not supported inside a function that returns:\n${Domain}.")
trait Dsl[-Keyword, Domain, +Value] extends PolyCont[Keyword, Domain, Value]

The domain-specific interpreter for Keyword in Domain, which is a dependent type type class that registers an asynchronous callback function, to handle the Value inside Keyword.

The domain-specific interpreter for Keyword in Domain, which is a dependent type type class that registers an asynchronous callback function, to handle the Value inside Keyword.

Type Params
Value

The value held inside Keyword.

Authors

杨博 (Yang Bo)

Example

Creating a collaborative DSL in Dsl.scala is easy. Only two steps are required:

  • Defining their domain-specific Keyword.
  • Implementing this Dsl type class, which is an interpreter for an Keyword.
Companion
object
object Dsl extends LowPriorityDsl0
Companion
class
object reset
Example

Suppose you are generating a random integer less than 100, whose first digit and second digit is different. A solution is generating integers in an infinite loop, and Return from the loop when the generated integer conforms with requirements.

import scala.util.Random
import scala.util.control.TailCalls
import scala.util.control.TailCalls.TailRec
import com.thoughtworks.dsl.reset
def randomInt(): TailRec[Int] = reset {
 while (true) {
   val r = Random.nextInt(100)
   if (r % 10 != r / 10) {
     !Return(TailCalls.done(r))
   }
 }
 throw new AssertionError("Unreachable code");
}
val r = randomInt().result
r should be < 100
r % 10 should not be r / 10

Since the Return keyword can automatically lift the return type, TailCalls.done can be omitted.

import scala.util.Random
import scala.util.control.TailCalls
import scala.util.control.TailCalls.TailRec
def randomInt(): TailRec[Int] = reset {
 while (true) {
   val r = Random.nextInt(100)
   if (r % 10 != r / 10) {
     !Return(r)
   }
 }
 throw new AssertionError("Unreachable code");
}
val r = randomInt().result
r should be < 100
r % 10 should not be r / 10