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
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
, 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
- Alphabetic
- By Inheritance
- Return
- Serializable
- Serializable
- Product
- Equals
- Keyword
- AnyVal
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new Return(returnValue: ReturnValue)
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
- def +(other: String): String
- def ->[B](y: B): (Return[ReturnValue], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
apply[Domain](handler: (Nothing) ⇒ Domain)(implicit dsl: Dsl[Return[ReturnValue], Domain, Nothing]): Domain
An alias to cpsApply.
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
final
def
cpsApply[Domain](handler: (Nothing) ⇒ Domain)(implicit dsl: Dsl[Return[ReturnValue], Domain, Nothing]): Domain
- Definition Classes
- Keyword
- Annotations
- @inline()
- def ensuring(cond: (Return[ReturnValue]) ⇒ Boolean, msg: ⇒ Any): Return[ReturnValue]
- def ensuring(cond: (Return[ReturnValue]) ⇒ Boolean): Return[ReturnValue]
- def ensuring(cond: Boolean, msg: ⇒ Any): Return[ReturnValue]
- def ensuring(cond: Boolean): Return[ReturnValue]
- def formatted(fmtstr: String): String
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- val returnValue: ReturnValue
- final def unary_!: Nothing
- def →[B](y: B): (Return[ReturnValue], B)