reset

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
class Object
trait Matchable
class Any

Type members

Classlikes

class *[Functor[_]]()

Value members

Concrete methods

inline
def *[Domain[_]]: *[Domain]
inline
def apply[Value](inline value: Value): Value
transparent inline
def reify[Value](inline value: Value): Any