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.
Author:
杨博 (Yang Bo)
- Source
- NullSafe.scala
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
- See also
NoneSafe for similar checks on scala.Options.
- Alphabetic
- By Inheritance
- NullSafe
- Serializable
- Serializable
- Product
- Equals
- AnyVal
- Any
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
Instance Constructors
- new NullSafe(nullable: A @com.thoughtworks.dsl.Dsl.reset)
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
##(): Int
- Definition Classes
- Any
- def +(other: String): String
- def ->[B](y: B): (NullSafe[A], B)
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- Any
-
final
def
?: NotNull[A]
- Annotations
- @shift() @compileTimeOnly( ... )
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
final
def
cpsApply[Domain >: Null](handler: (NotNull[A]) ⇒ Domain @com.thoughtworks.dsl.Dsl.reset): Domain @com.thoughtworks.dsl.Dsl.reset
- Annotations
- @inline()
- def ensuring(cond: (NullSafe[A]) ⇒ Boolean, msg: ⇒ Any): NullSafe[A]
- def ensuring(cond: (NullSafe[A]) ⇒ Boolean): NullSafe[A]
- def ensuring(cond: Boolean, msg: ⇒ Any): NullSafe[A]
- def ensuring(cond: Boolean): NullSafe[A]
- def formatted(fmtstr: String): String
-
def
getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- val nullable: A @com.thoughtworks.dsl.Dsl.reset
- def →[B](y: B): (NullSafe[A], B)