Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow x[] for get(x::Nullable) #18766

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ function show{T}(io::IO, x::Nullable{T})
end

"""
get(x::Nullable[, y])
get(x::Nullable, y)

Attempt to access the value of `x`. Returns the value if it is present;
otherwise, returns `y` if provided, or throws a `NullException` if not.
otherwise, returns `y`.
"""
@inline function get{S,T}(x::Nullable{S}, y::T)
if isbits(S)
Expand All @@ -59,7 +59,22 @@ otherwise, returns `y` if provided, or throws a `NullException` if not.
end
end

get(x::Nullable) = isnull(x) ? throw(NullException()) : x.value
"""
getindex(x::Nullable)

Attempt to access the value of `x`. Throw a `NullException` if the value is not
present. Usually, this is written as `x[]`.
"""
getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value

"""
get(x::Nullable)

Attempt to access the value of `x`. Throw a `NullException` if the value is not
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throws?

present. Equivalent to `getindex(x::Nullable)`, which can alternatively be
written as `x[]`.
"""
get(x::Nullable) = x[]

"""
unsafe_get(x)
Expand Down
10 changes: 8 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,17 @@ Nullables

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``\ .

.. function:: get(x::Nullable[, y])
.. function:: get(x::Nullable, y)

.. Docstring generated from Julia source

Attempt to access the value of ``x``\ . Returns the value if it is present; otherwise, returns ``y`` if provided, or throws a ``NullException`` if not.
Attempt to access the value of ``x``\ . Returns the value if it is present; otherwise, returns ``y``\ .

.. function:: getindex(x::Nullable)

.. Docstring generated from Julia source

Attempt to access the value of ``x``\ . Throw a ``NullException`` if the value is not present. Usually, this is written as ``x[]``\ .

.. function:: isnull(x)

Expand Down
6 changes: 4 additions & 2 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ for T in types
x3 = Nullable(one(T))

@test_throws NullException get(x1)
@test get(x2) === zero(T)
@test get(x3) === one(T)
@test_throws NullException x1[]
@test get(x2) === x2[] === zero(T)
@test get(x3) === x3[] === one(T)
end

@test_throws NullException get(Nullable())
@test_throws NullException Nullable()[]

# get{S, T}(x::Nullable{S}, y::T)
for T in types
Expand Down