Class/Object

com.thoughtworks.dsl.keywords

NullSafe

Related Docs: object NullSafe | package keywords

Permalink

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
Examples:
  1. 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)
  2. ,
  3. 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 selecting left, right or value on a null 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.?.right.?.value should be(null)

    The entire expression will be null if one of ? is performed on a null 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)
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.

Linear Supertypes
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. NullSafe
  2. Serializable
  3. Serializable
  4. Product
  5. Equals
  6. AnyVal
  7. 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 NullSafe(nullable: A @com.thoughtworks.dsl.Dsl.reset)

    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 NullSafe[A] to any2stringadd[NullSafe[A]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (NullSafe[A], B)

    Permalink
    Implicit information
    This member is added by an implicit conversion from NullSafe[A] to ArrowAssoc[NullSafe[A]] 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 ?: NotNull[A]

    Permalink
    Annotations
    @shift() @compileTimeOnly( ... )
  7. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  8. final def cpsApply[Domain >: Null](handler: (NotNull[A]) ⇒ Domain @com.thoughtworks.dsl.Dsl.reset): Domain @com.thoughtworks.dsl.Dsl.reset

    Permalink
    Annotations
    @inline()
  9. def ensuring(cond: (NullSafe[A]) ⇒ Boolean, msg: ⇒ Any): NullSafe[A]

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

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

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

    Permalink
    Implicit information
    This member is added by an implicit conversion from NullSafe[A] to Ensuring[NullSafe[A]] 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 NullSafe[A] to StringFormat[NullSafe[A]] 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 nullable: A @com.thoughtworks.dsl.Dsl.reset

    Permalink
  17. def [B](y: B): (NullSafe[A], B)

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

Inherited from Serializable

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from AnyVal

Inherited from Any

Inherited by implicit conversion any2stringadd from NullSafe[A] to any2stringadd[NullSafe[A]]

Inherited by implicit conversion StringFormat from NullSafe[A] to StringFormat[NullSafe[A]]

Inherited by implicit conversion Ensuring from NullSafe[A] to Ensuring[NullSafe[A]]

Inherited by implicit conversion ArrowAssoc from NullSafe[A] to ArrowAssoc[NullSafe[A]]

Ungrouped