com.thoughtworks.dsl.keywords
NullSafe is a keyword to perform null check.
null
杨博 (Yang Bo)
You can use ? annotation to represent a nullable value.
import com.thoughtworks.dsl.keywords.NullSafe._ case class Tree(left: Tree @ $qmark = null, right: Tree @ $qmark = null, value: String @ $qmark = null) val root: Tree @ $qmark = Tree( left = Tree( left = Tree(value = "left-left"), right = Tree(value = "left-right") ), right = Tree(value = "right") )
Normal . is not null safe, when selecting nullable left, right or value.
.
left
right
value
a[NullPointerException] should be thrownBy { root.right.left.right.value }
The above code throws an exception because root.right.left is null. The exception can be avoided by using ? on a nullable value:
root.right.left
root.?.right.?.left.?.value should be(null)
The entire expression is null if one of ? is performed on a null value.
?
("Hello " + ("world " + root.?.right.?.left.?.value)) should be("Hello world null") ("Hello " + (("world " + root.?.right.?.left.?.value.?): @ $qmark)) should be("Hello null") (("Hello " + ("world " + root.?.right.?.left.?.value.?)): @ $qmark) should be(null)
NullSafe is a keyword to perform
null
check.Author:
杨博 (Yang Bo)
You can use ? annotation to represent a nullable value.
Normal
.
is not null safe, when selecting nullableleft
,right
orvalue
.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.?.value should be(null)
The entire expression is
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 ?.