diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index 0e370afc6af63..4d0ac2f959a16 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -197,15 +197,6 @@ Subtraction operator. """ -(x, y) -""" - Nullable(x) - -Wrap value `x` in an object of type `Nullable`, which indicates whether a value is present. -`Nullable(x)` yields a non-empty wrapper, and `Nullable{T}()` yields an empty instance of a -wrapper that might contain a value of type `T`. -""" -Nullable - """ bits(n) diff --git a/base/nullable.jl b/base/nullable.jl index dfcec4f68dd3e..9c4c9ca081c81 100644 --- a/base/nullable.jl +++ b/base/nullable.jl @@ -3,6 +3,34 @@ immutable NullException <: Exception end +""" + Nullable(x, hasvalue::Bool=true) + +Wrap value `x` in an object of type `Nullable`, which indicates whether a value is present. +`Nullable(x)` yields a non-empty wrapper and `Nullable{T}()` yields an empty instance of a +wrapper that might contain a value of type `T`. + +`Nullable(x, false)` yields `Nullable{typeof(x)}()` with `x` stored in the result's `value` +field. + +# Examples + +```jldoctest +julia> Nullable(1) +Nullable{Int64}(1) + +julia> Nullable{Int64}() +Nullable{Int64}() + +julia> Nullable(1, false) +Nullable{Int64}() + +julia> dump(Nullable(1, false)) +Nullable{Int64} + hasvalue: Bool false + value: Int64 1 +``` +""" Nullable{T}(value::T, hasvalue::Bool=true) = Nullable{T}(value, hasvalue) Nullable() = Nullable{Union{}}() @@ -100,18 +128,20 @@ unsafe_get(x) = x Return whether or not `x` is null for [`Nullable`](:obj:`Nullable`) `x`; return `false` for all other `x`. +# Examples + ```jldoctest julia> x = Nullable(1, false) -Nullable{Int64}(1) +Nullable{Int64}() julia> isnull(x) -false +true julia> x = Nullable(1, true) -Nullable{Int64}() +Nullable{Int64}(1) julia> isnull(x) -true +false julia> x = 1 1 diff --git a/doc/manual/types.rst b/doc/manual/types.rst index d4e624ccad442..1b60cc81db946 100644 --- a/doc/manual/types.rst +++ b/doc/manual/types.rst @@ -1487,7 +1487,7 @@ You can safely access the value of a :obj:`Nullable` object using :func:`get`: julia> get(Nullable{Float64}()) ERROR: NullException() - in get(::Nullable{Float64}) at ./nullable.jl:62 + in get(::Nullable{Float64}) at ./nullable.jl:90 ... julia> get(Nullable(1.0)) diff --git a/doc/stdlib/base.rst b/doc/stdlib/base.rst index 2d721ae813e34..5c0236e02e1d4 100644 --- a/doc/stdlib/base.rst +++ b/doc/stdlib/base.rst @@ -815,11 +815,31 @@ Syntax Nullables --------- -.. function:: Nullable(x) +.. function:: Nullable(x, hasvalue::Bool=true) .. Docstring generated from Julia source - Wrap value ``x`` in an object of type ``Nullable``\ , which indicates whether a value is present. ``Nullable(x)`` yields a non-empty wrapper, and ``Nullable{T}()`` yields an empty instance of a wrapper that might contain a value of type ``T``\ . + Wrap value ``x`` in an object of type ``Nullable``\ , which indicates whether a value is present. ``Nullable(x)`` yields a non-empty wrapper and ``Nullable{T}()`` yields an empty instance of a wrapper that might contain a value of type ``T``\ . + + ``Nullable(x, false)`` yields ``Nullable{typeof(x)}()`` with ``x`` stored in the result's ``value`` field. + + **Examples** + + .. doctest:: + + julia> Nullable(1) + Nullable{Int64}(1) + + julia> Nullable{Int64}() + Nullable{Int64}() + + julia> Nullable(1, false) + Nullable{Int64}() + + julia> dump(Nullable(1, false)) + Nullable{Int64} + hasvalue: Bool false + value: Int64 1 .. function:: get(x::Nullable[, y]) @@ -833,19 +853,21 @@ Nullables Return whether or not ``x`` is null for :obj:`Nullable` ``x``\ ; return ``false`` for all other ``x``\ . + **Examples** + .. doctest:: julia> x = Nullable(1, false) - Nullable{Int64}(1) + Nullable{Int64}() julia> isnull(x) - false + true julia> x = Nullable(1, true) - Nullable{Int64}() + Nullable{Int64}(1) julia> isnull(x) - true + false julia> x = 1 1