Class/Object

com.thoughtworks.dsl.keywords

Return

Related Docs: object Return | package keywords

Permalink

final case class Return[ReturnValue](returnValue: ReturnValue) extends AnyVal with Keyword[Return[ReturnValue], Nothing] with Product with Serializable

A Dsl.Keyword to early return a lifted value from the enclosing function.

Author:

杨博 (Yang Bo)

Source
Return.scala
Examples:
  1. Since this 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] = {
      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
  2. ,
  3. 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
    def randomInt(): TailRec[Int] = {
      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
Linear Supertypes
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Return
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. Keyword
  7. AnyVal
  8. Any
Implicitly
  1. by any2stringadd
  2. by StringFormat
  3. by Ensuring
  4. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Return(returnValue: ReturnValue)

    Permalink

Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    Any
  3. def +(other: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to any2stringadd[Return[ReturnValue]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (Return[ReturnValue], B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to ArrowAssoc[Return[ReturnValue]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    Any
  6. final def apply[Domain](handler: (Nothing) ⇒ Domain)(implicit dsl: Dsl[Return[ReturnValue], Domain, Nothing]): Domain

    Permalink

    An alias to cpsApply.

    An alias to cpsApply.

    Definition Classes
    Keyword
    Annotations
    @inline()
  7. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  8. final def cpsApply[Domain](handler: (Nothing) ⇒ Domain)(implicit dsl: Dsl[Return[ReturnValue], Domain, Nothing]): Domain

    Permalink
    Definition Classes
    Keyword
    Annotations
    @inline()
  9. def ensuring(cond: (Return[ReturnValue]) ⇒ Boolean, msg: ⇒ Any): Return[ReturnValue]

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to Ensuring[Return[ReturnValue]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  10. def ensuring(cond: (Return[ReturnValue]) ⇒ Boolean): Return[ReturnValue]

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to Ensuring[Return[ReturnValue]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  11. def ensuring(cond: Boolean, msg: ⇒ Any): Return[ReturnValue]

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to Ensuring[Return[ReturnValue]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  12. def ensuring(cond: Boolean): Return[ReturnValue]

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to Ensuring[Return[ReturnValue]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  13. def formatted(fmtstr: String): String

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to StringFormat[Return[ReturnValue]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  14. def getClass(): Class[_ <: AnyVal]

    Permalink
    Definition Classes
    AnyVal → Any
  15. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  16. val returnValue: ReturnValue

    Permalink
  17. final def unary_!: Nothing

    Permalink
    Definition Classes
    Keyword
    Annotations
    @shift() @compileTimeOnly( ... )
  18. def [B](y: B): (Return[ReturnValue], B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from Return[ReturnValue] to ArrowAssoc[Return[ReturnValue]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from Keyword[Return[ReturnValue], Nothing]

Inherited from AnyVal

Inherited from Any

Inherited by implicit conversion any2stringadd from Return[ReturnValue] to any2stringadd[Return[ReturnValue]]

Inherited by implicit conversion StringFormat from Return[ReturnValue] to StringFormat[Return[ReturnValue]]

Inherited by implicit conversion Ensuring from Return[ReturnValue] to Ensuring[Return[ReturnValue]]

Inherited by implicit conversion ArrowAssoc from Return[ReturnValue] to ArrowAssoc[Return[ReturnValue]]

Ungrouped