com.thoughtworks.dsl
package com.thoughtworks.dsl
Type members
Classlikes
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
Types
@implicitNotFound("The keyword:\n ${Keyword}\nis not supported inside a function that returns:\n${Domain}.")
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