package keywords
Contains built-in domain-specific Keywords and their corresponding interpreters.
- Source
- package.scala
- Alphabetic
- By Inheritance
- keywords
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- trait AsynchronousIo[Value] extends Keyword[AsynchronousIo[Value], Value]
- final case class Await[Value](future: Future[Value]) extends AnyVal with Keyword[Await[Value], Value] with Product with Serializable
- final case class Catch[Domain, Value](block: !![Domain, Value], catcher: Catcher[!![Domain, Value]]) extends Keyword[Catch[Domain, Value], Value] with Product with Serializable
Author:
杨博 (Yang Bo)
- final case class Get[S]() extends Keyword[Get[S], S] with Product with Serializable
Author:
杨博 (Yang Bo)
- See also
- final case class Monadic[F[_], A](fa: F[A]) extends Keyword[Monadic[F, A], A] with Product with Serializable
A keyword for extracting monadic value from the monadic expression fa.
A keyword for extracting monadic value from the monadic expression fa.
- To do
Monadic should be a scala.AnyVal after https://github.com/scala/bug/issues/10595 is resolved.
- See also
com.thoughtworks.dsl.domains.cats for using this Monadic keyword with cats.Monad.
com.thoughtworks.dsl.domains.scalaz for using this Monadic keyword with scalaz.Monad.
- final case class NoneSafe[A](option: Option[A]) extends AnyVal with Keyword[NoneSafe[A], A] with Product with Serializable
- final case class NullSafe[A <: AnyRef](nullable: A @com.thoughtworks.dsl.Dsl.reset) extends AnyVal with Product with Serializable
NullSafe is a keyword to perform
null
check.NullSafe is a keyword to perform
null
check.Author:
杨博 (Yang Bo)
You can use ? annotation to represent a nullable value.
import com.thoughtworks.dsl.keywords.NullSafe._ case class Tree(left: Tree @ ? = null, right: Tree @ ? = null, value: String @ ? = null) val root: Tree @ ? = Tree( left = Tree( left = Tree(value = "left-left"), right = Tree(value = "left-right") ), right = Tree(value = "right") )
A normal
.
is not null safe, when selectingleft
,right
orvalue
on anull
value.a[NullPointerException] should be thrownBy { root.right.left.right.value }
The above code throws an exception because
root.right.left
isnull
. The exception can be avoided by using ? on a nullable value:root.?.right.?.left.?.right.?.value should be(null)
The entire expression will be
null
if one of ? is performed on anull
value.
The boundary of a null safe operator ? is the nearest enclosing expression whose type is annotated as@ ?
.("Hello " + ("world " + root.?.right.?.left.?.value)) should be("Hello world null") ("Hello " + (("world " + root.?.right.?.left.?.value.?): @ ?)) should be("Hello null") (("Hello " + ("world " + root.?.right.?.left.?.value.?)): @ ?) should be(null)
, The ? operator usually works with Java libraries that may produce
null
.import com.thoughtworks.dsl.keywords.NullSafe._ val myMap = new java.util.HashMap[String, String](); ((myMap.get("key1").? + myMap.get("key2").?): @ ?) should be(null)
- Note
The ? operator is only available on nullable values. A type is considered as nullable if it is a reference type, no matter it is annotated as
@ ?
or not.import com.thoughtworks.dsl.keywords.NullSafe._ val explicitNullable: String @ ? = null ((explicitNullable.? + " Doe") : @ ?) should be(null)
val implicitNullable: String = null ((implicitNullable.? + " Doe") : @ ?) should be(null)
A type is considered as not nullable if it is a value type.
val implicitNotNullable: Int = 0 "(implicitNotNullable.? + 42) : @ ?" shouldNot compile
Alternatively, a type can be considered as not nullable by explicitly converting it to NotNull.
val explicitNotNullable: NotNull[String] = NotNull("John") """(explicitNotNullable.? + " Doe") : @ ?""" shouldNot compile
Examples: - final case class Put[S](value: S) extends AnyVal with Keyword[Put[S], Unit] with Product with Serializable
Put is a Dsl.Keyword to put the value to the context.
Put is a Dsl.Keyword to put the value to the context.
Purely functional programming languages usually do not support native first-class mutable variables. In those languages, mutable states can be implemented in state monads.
Put and Get are the Dsl-based replacements of state monads.
We use unary function as the domain of mutable state. The parameter of the unary function can be read from the Get keyword, and changed by the Put keyword.
Author:
杨博 (Yang Bo)
The following example creates a function that accepts a string parameter and returns the upper-cased last character of the parameter.
def upperCasedLastCharacter: String => Char = { val initialValue = !Get[String]() !Put(initialValue.toUpperCase) val upperCased = !Get[String]() Function.const(upperCased.last) }
For example, given a string of
foo
, the upper-cased last character should beO
.// Output: O upperCasedLastCharacter("foo") should be('O')
, Put and Get support multiple states. The following code creates a formatter that Put parts of content into a
Vector[Any]
of string buffers.def formatter: Double => Int => Vector[Any] => String = { !Put(!Get[Vector[Any]] :+ "x=") !Put(!Get[Vector[Any]] :+ !Get[Double]) !Put(!Get[Vector[Any]] :+ ",y=") !Put(!Get[Vector[Any]] :+ !Get[Int]) !Return((!Get[Vector[Any]]).mkString) } formatter(0.5)(42)(Vector.empty) should be("x=0.5,y=42")
- See also
Examples: - 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.
A Dsl.Keyword to early return a lifted value from the enclosing function.
Author:
杨博 (Yang Bo)
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
Examples: - final case class Shift[Domain, Value](continuation: !![Domain, Value]) extends AnyVal with Keyword[Shift[Domain, Value], Value] with Product with Serializable
Author:
杨博 (Yang Bo)
- final case class Using[R <: AutoCloseable](open: () ⇒ R) extends AnyVal with Keyword[Using[R], R] with Product with Serializable
Author:
杨博 (Yang Bo)
Value Members
- object AsynchronousIo
- object Await extends Serializable
- object Catch extends LowPriorityCatch0 with Serializable
- object Get extends Serializable
- object Monadic extends Serializable
- object NoneSafe extends Serializable
- object NullSafe extends Serializable
- object Put extends Serializable
- object Return extends Serializable
- object Shift extends LowPriorityShift0 with Serializable
- object Using extends Serializable