com.thoughtworks.dsl.macros

Members list

Concise view

Type members

Classlikes

trait Reset

Macros to translate the reset operator of a delimited continuation, where all the control flows are an AST of keywords, interpreted by the Dsl type class.

Macros to translate the reset operator of a delimited continuation, where all the control flows are an AST of keywords, interpreted by the Dsl type class.

Attributes

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.macros.Reset.Default.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
import com.thoughtworks.dsl.macros.Reset.Default.reset
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
Companion:
object
Graph
Supertypes
class Object
trait Matchable
class Any
object Reset

Attributes

Companion:
trait
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
Reset.type