Optional data types in the use the question mark at the end (for example, String?
) or the notation Optional<...>
.
The following operations are most often performed on optional data types:
- IS NULL: Matching an empty value
- : Leaves the filled values unchanged and replaces
NULL
with the default value that follows - JUST Change the data type to the optional type of the current one,
T
converts toT?
- : Create an empty value with the specified type.
(nullable) isn’t a property of a data type or column, but a container type where containers can be arbitrarily nested into each other. For example, a column with the type Optional<Optional<Boolean>>
can accept 4 values: NULL
of the overarching container, NULL
of the inner container, TRUE
, and FALSE
. The above-declared type differs from List<List<Boolean>>
, because it uses NULL
as an empty list, and you can’t put more than one non-null element in it. You can also use Optional<Optional<T>>
as a key in the dictionary () with Optional<T>
values. Using this type of result data, you can distinguish between a NULL
value in the dictionary and a missing key.
Result:
Logical and arithmetic operations with NULL
The NULL
literal has a separate singular Null
type and can be implicitly converted to any optional type (for example, the nested type OptionalOptional<T>...>>
). In ANSI SQL, NULL
means “an unknown value”, that’s why logical and arithmetic operations involving NULL
or empty Optional
have certain specifics.
Examples