Packages

  • package root
    Definition Classes
    root
  • package scala
    Definition Classes
    root
  • package util
    Definition Classes
    scala
  • package continuations

    Delimited continuations are a feature for modifying the usual control flow of a program.

    Delimited continuations are a feature for modifying the usual control flow of a program. To use continuations, provide the option -P:continuations:enable to the Scala compiler or REPL to activate the compiler plugin.

    Below is an example of using continuations to suspend execution while awaiting user input. Similar facilities are used in so-called continuation-based web frameworks.

    def go =
      reset {
        println("Welcome!")
        val first = ask("Please give me a number")
        val second = ask("Please enter another number")
        printf("The sum of your numbers is: %d\n", first + second)
      }

    The reset is provided by this package and delimits the extent of the transformation. The ask is a function that will be defined below. Its effect is to issue a prompt and then suspend execution awaiting user input. Once the user provides an input value, execution of the suspended block resumes.

    val sessions = new HashMap[UUID, Int=>Unit]
    def ask(prompt: String): Int @cps[Unit] =
      shift {
        k: (Int => Unit) => {
          val id = uuidGen
          printf("%s\nrespond with: submit(0x%x, ...)\n", prompt, id)
          sessions += id -> k
        }
      }

    The type of ask includes a @cps annotation which drives the transformation. The type signature Int @cps[Unit] means that ask should be used in a context requiring an Int, but actually it will suspend and return Unit.

    The computation leading up to the first ask is executed normally. The remainder of the reset block is wrapped into a closure that is passed as the parameter k to the shift function, which can then decide whether and how to execute the continuation. In this example, the continuation is stored in a sessions map for later execution. This continuation includes a second call to ask, which is treated likewise once the execution resumes.

    CPS Annotation

    The aforementioned @cps[A] annotation is an alias for the more general @cpsParam[B,C] where B=C. The type A @cpsParam[B,C] describes a term which yields a value of type A within an evaluation context producing a value of type B. After the CPS transformation, this return type is modified to C.

    The @cpsParam annotations are introduced by shift blocks, and propagate via the return types to the dynamically enclosing context. The propagation stops upon reaching a reset block.

    Definition Classes
    util
  • ControlContext
  • cpsParam

class cpsParam[-B, +C] extends Annotation with StaticAnnotation with TypeConstraint

This annotation is used to mark a parameter as part of a continuation context.

The type A @cpsParam[B,C] is desugared to ControlContext[A,B,C] at compile time.

B

The type of computation state after computation has executed, and before control is returned to the shift.

C

The eventual return type of this delimited compuation.

See also

scala.util.continuations.ControlContext

Linear Supertypes
TypeConstraint, StaticAnnotation, Annotation, AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. cpsParam
  2. TypeConstraint
  3. StaticAnnotation
  4. Annotation
  5. AnyRef
  6. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new cpsParam()

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def clone(): AnyRef
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  8. def finalize(): Unit
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  9. final def getClass(): Class[_]
    Definition Classes
    AnyRef → Any
  10. def hashCode(): Int
    Definition Classes
    AnyRef → Any
  11. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  13. final def notify(): Unit
    Definition Classes
    AnyRef
  14. final def notifyAll(): Unit
    Definition Classes
    AnyRef
  15. final def synchronized[T0](arg0: ⇒ T0): T0
    Definition Classes
    AnyRef
  16. def toString(): String
    Definition Classes
    AnyRef → Any
  17. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from TypeConstraint

Inherited from StaticAnnotation

Inherited from Annotation

Inherited from AnyRef

Inherited from Any

Ungrouped