syntax

object syntax
class Object
trait Matchable
class Any

Extensions

Extensions

extension (a: A | Null)
final inline def cata[A](ifEmpty: => B, f: A => B): B
final inline def collect[A](pf: PartialFunction[A, B]): B | Null

Returns a $Nullable containing the result of applying pf to a Nullable's contained value, '''if''' a option is nonempty '''and''' pf is defined for that value. Returns $null otherwise.

Returns a $Nullable containing the result of applying pf to a Nullable's contained value, '''if''' a option is nonempty '''and''' pf is defined for that value. Returns $null otherwise.

Value Params
pf

the partial function.

Returns

the result of applying pf to a Nullable's value (if possible), or $null.

Example
// Returns Nullable(HTTP) because the partial function covers the case.
Nullable("http") collect {case "http" => "HTTP"}
// Returns null because the partial function doesn't cover the case.
Nullable("ftp") collect {case "http" => "HTTP"}
// Returns null because the option is empty. There is no value to pass to the partial function.
null collect {case value => value}
final def contains[A](elem: A1): Boolean

Tests whether the option contains a given value as an element.

Tests whether the option contains a given value as an element.

Value Params
elem

the element to test.

Returns

true if the option has an element that is equal (as determined by ==) to elem, false otherwise.

Example
// Returns true because Nullable instance contains string "Nullablething" which equals "Nullablething".
Nullable("Nullablething") contains "Nullablething"
// Returns false because "Nullablething" != "anything".
Nullable("Nullablething") contains "anything"
// Returns false when method called on null.
null contains "anything"
final inline def exists[A](p: A => Boolean): Boolean

Returns true if a value is not null '''and''' the predicate $p returns true when applied to the value. Otherwise, returns false.

Returns true if a value is not null '''and''' the predicate $p returns true when applied to the value. Otherwise, returns false.

Value Params
p

the predicate to test

final inline def filter[A](p: A => Boolean): A | Null
final inline def filterNot[A](p: A => Boolean): A | Null

Returns a Nullable if it is nonempty '''and''' applying the predicate $p to a Nullable's value returns false. Otherwise, return $null.

Returns a Nullable if it is nonempty '''and''' applying the predicate $p to a Nullable's value returns false. Otherwise, return $null.

Value Params
p

the predicate used for testing.

inline def flatMap[A](f: A => B)(using `evidence$4`: Nullable[B]): B | Null
final inline def fold[A](ifEmpty: => B)(f: A => B): B

Returns the result of applying $f to a Nullable's value if the Nullable is nonempty. Otherwise, evaluates expression ifEmpty.

Returns the result of applying $f to a Nullable's value if the Nullable is nonempty. Otherwise, evaluates expression ifEmpty.

a is equivalent to:

nullable map f getOrElse ifEmpty
Value Params
f

the function to apply if nonempty.

ifEmpty

the expression to evaluate if empty.

final inline def forall[A](p: A => Boolean): Boolean

Returns true if a value is null '''or''' the predicate $p returns true when applied to the value.

Returns true if a value is null '''or''' the predicate $p returns true when applied to the value.

Value Params
p

the predicate to test

final inline def foreach[A](f: A => U): Unit

Apply the given procedure $f to the option's value, if it is nonempty. Otherwise, do nothing.

Apply the given procedure $f to the option's value, if it is nonempty. Otherwise, do nothing.

Value Params
f

the procedure to apply.

See also

map

flatMap

final inline def getOrElse[A](default: => B): B

Returns the wrapped value, unless it's null, in which case it returns the result of evaluating default.

Returns the wrapped value, unless it's null, in which case it returns the result of evaluating default.

Value Params
default

the default (fallback) expression.

inline def isDefined[A]: Boolean
inline def isEmpty[A]: Boolean

Docs and some of the code below has been adaped from scala.Option whose license is:

Docs and some of the code below has been adaped from scala.Option whose license is:

Scala (https://www.scala-lang.org)

Copyright EPFL and Lightbend, Inc.

Licensed under Apache License 2.0 (http://www.apache.org/licenses/LICENSE-2.0)

def iterator[A]: Iterator[A]

Returns a singleton iterator returning the Nullable's value if it is nonempty, or an empty iterator if the option is empty.

Returns a singleton iterator returning the Nullable's value if it is nonempty, or an empty iterator if the option is empty.

inline def map[A](f: A => B)(using `evidence$3`: NotNull[B]): B | Null
final def nonEmpty[A]: Boolean

Returns false if the option is $null, true otherwise.

Returns false if the option is $null, true otherwise.

final inline def orElse[A](alternative: => B | Null): B | Null

Returns a Nullable if it is nonempty, otherwise return the result of evaluating alternative.

Returns a Nullable if it is nonempty, otherwise return the result of evaluating alternative.

Value Params
alternative

the alternative expression.

final inline def orFail[A]: A
final inline def toLeft[A](right: => X): Either[A, X]

Returns a scala.util.Right containing the given argument right if a is empty, or a scala.util.Left containing a Nullable's value if a Nullable is nonempty.

Returns a scala.util.Right containing the given argument right if a is empty, or a scala.util.Left containing a Nullable's value if a Nullable is nonempty.

Value Params
right

the expression to evaluate and return if a is empty

See also

toRight

def toList[A]: List[A]

Returns a singleton list containing the Nullable's value if it is nonempty, or the empty list if the Nullable is empty.

Returns a singleton list containing the Nullable's value if it is nonempty, or the empty list if the Nullable is empty.

def toOption[A]: Option[A]
final inline def toRight[A](left: => X): Either[X, A]

Returns a scala.util.Left containing the given argument left if a Nullable is empty, or a scala.util.Right containing a Nullable's value if a is nonempty.

Returns a scala.util.Left containing the given argument left if a Nullable is empty, or a scala.util.Right containing a Nullable's value if a is nonempty.

Value Params
left

the expression to evaluate and return if a is empty

See also

toLeft

final def unzip[A](asPair: A <:< (A1, A2)): (A1 | Null, A2 | Null)

Converts a Nullable of a pair into a Nullable of the first element and a Nullable of the second element.

Converts a Nullable of a pair into a Nullable of the first element and a Nullable of the second element.

Type Params
A1

the type of the first half of the element pair

A2

the type of the second half of the element pair

Value Params
asPair

an implicit conversion which asserts that the element type of a Option is a pair.

Returns

a pair of Options, containing, respectively, the first and second half of the element pair of a Option.

final def unzip3[A](asTriple: A <:< (A1, A2, A3)): (A1 | Null, A2 | Null, A3 | Null)

Converts a Nullable of a triple into three Options, one containing the element from each position of the triple.

Converts a Nullable of a triple into three Options, one containing the element from each position of the triple.

Type Params
A1

the type of the first of three elements in the triple

A2

the type of the second of three elements in the triple

A3

the type of the third of three elements in the triple

Value Params
asTriple

an implicit conversion which asserts that the element type of a Option is a triple.

Returns

a triple of Options, containing, respectively, the first, second, and third elements from the element triple of a Option.

final inline def withFilter[A](p: A => Boolean): A | Null
final def zip[A](b: B | Null): (A, B) | Null

Returns a $Nullable formed from a option and another option by combining the corresponding elements in a pair. If either of the two options is empty, $null is returned.

Returns a $Nullable formed from a option and another option by combining the corresponding elements in a pair. If either of the two options is empty, $null is returned.

Value Params
that

the options which is going to be zipped

Example
// Returns Nullable(("foo", "bar")) because both options are nonempty.
Nullable("foo") zip Nullable("bar")
// Returns null because `that` option is empty.
Nullable("foo") zip null
// Returns null because `a` option is empty.
null zip Nullable("bar")