|
| 1 | +.. _man-nullable-types: |
| 2 | + |
| 3 | +******************************************* |
| 4 | +Nullable Types: Representing Missing Values |
| 5 | +******************************************* |
| 6 | + |
| 7 | +In many settings, you need to interact with a value of type ``T`` that may or |
| 8 | +may not exist. To handle these settings, Julia provides a parametric type |
| 9 | +called ``Nullable{T}``, which can be thought of as a specialized container |
| 10 | +type that can contain either zero or one values. ``Nullable{T}`` provides a |
| 11 | +minimal interface designed to ensure that interactions with missing values |
| 12 | +are safe. At present, the interface consists of four possible interactions: |
| 13 | + |
| 14 | +- Construct a ``Nullable`` object. |
| 15 | +- Check if an ``Nullable`` object has a missing value. |
| 16 | +- Access the value of a ``Nullable`` object with a guarantee that a |
| 17 | + ``NullException`` will be thrown if the object's value is missing. |
| 18 | +- Access the value of a ``Nullable`` object with a guarantee that a default |
| 19 | + value of type ``T`` will be returned if the object's value is missing. |
| 20 | + |
| 21 | +Constructing ``Nullable`` objects |
| 22 | +--------------------------------- |
| 23 | + |
| 24 | +To construct an object representing a missing value of type ``T``, use the |
| 25 | +``Nullable{T}()`` function: |
| 26 | + |
| 27 | +.. doctest:: |
| 28 | + |
| 29 | + x1 = Nullable{Int}() |
| 30 | + x2 = Nullable{Float64}() |
| 31 | + x3 = Nullable{Vector{Int}}() |
| 32 | + |
| 33 | +To construct an object representing a non-missing value of type ``T``, use the |
| 34 | +``Nullable(x::T)`` function: |
| 35 | + |
| 36 | +.. doctest:: |
| 37 | + |
| 38 | + x1 = Nullable(1) |
| 39 | + x2 = Nullable(1.0) |
| 40 | + x3 = Nullable([1, 2, 3]) |
| 41 | + |
| 42 | +Note the core distinction between these two ways of constructing a ``Nullable`` |
| 43 | +object: in one style, you provide a type, ``T``, as a function parameter; in |
| 44 | +the other style, you provide a single value of type ``T`` as an argument. |
| 45 | + |
| 46 | +Checking if an ``Nullable`` object has a value |
| 47 | +---------------------------------------------- |
| 48 | + |
| 49 | +You can check if a ``Nullable`` object has any value using the ``isnull`` |
| 50 | +function: |
| 51 | + |
| 52 | +.. doctest:: |
| 53 | + |
| 54 | + isnull(Nullable{Float64}()) |
| 55 | + isnull(Nullable(0.0)) |
| 56 | + |
| 57 | +Safely accessing the value of an ``Nullable`` object |
| 58 | +---------------------------------------------------- |
| 59 | + |
| 60 | +You can safely access the value of an ``Nullable`` object using the ``get`` |
| 61 | +function: |
| 62 | + |
| 63 | +.. doctest:: |
| 64 | + |
| 65 | + get(Nullable{Float64}()) |
| 66 | + get(Nullable(1.0)) |
| 67 | + |
| 68 | +If the value is not present, as it would be for ``Nullable{Float64}``, a |
| 69 | +``NullException`` error will be thrown. The error-throwing nature of the |
| 70 | +``get`` function ensures that any attempt to access a missing value immediately |
| 71 | +fails. |
| 72 | + |
| 73 | +In cases for which a reasonable default value exists that could be used |
| 74 | +when a ``Nullable`` object's value turns out to be missing, you can provide this |
| 75 | +default value as a second argument to ``get``: |
| 76 | + |
| 77 | +.. doctest:: |
| 78 | + |
| 79 | + get(Nullable{Float64}(), 0) |
| 80 | + get(Nullable(1.0), 0) |
| 81 | + |
| 82 | +Note that this default value will automatically be converted to the type of |
| 83 | +the ``Nullable`` object that you attempt to access using the ``get`` function. |
| 84 | +For example, in the code shown above the value ``0`` would be automatically |
| 85 | +converted to a ``Float64`` value before being returned. The presence of default |
| 86 | +replacement values makes it easy to use the ``get`` function to write |
| 87 | +type-stable code that interacts with sources of potentially missing values. |
0 commit comments