Skip to content

Commit

Permalink
make nothing convertible to an empty Nullable.
Browse files Browse the repository at this point in the history
add help for nothing and Nullable

ref #9364
alternative to c966812
  • Loading branch information
JeffBezanson committed Feb 18, 2015
1 parent 30cc131 commit ff3a471
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ immutable NullException <: Exception
end

Nullable{T}(value::T) = Nullable{T}(value)
Nullable() = Nullable{Union()}()

eltype{T}(::Type{Nullable{T}}) = T

Expand All @@ -19,6 +20,9 @@ end

convert{T}(::Type{Nullable{T}}, x::T) = Nullable{T}(x)

convert{T}(::Type{Nullable{T}}, ::Void) = Nullable{T}()
convert( ::Type{Nullable }, ::Void) = Nullable{Union()}()

function show{T}(io::IO, x::Nullable{T})
if x.isnull
@printf(io, "Nullable{%s}()", repr(T))
Expand Down
6 changes: 6 additions & 0 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ Syntax
Nullables
---------

.. function:: 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``.

.. function:: get(x)

Attempt to access the value of the ``Nullable`` object, ``x``. Returns the
Expand Down
6 changes: 6 additions & 0 deletions doc/stdlib/constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
Constants
=========

.. data:: nothing

The singleton instance of type ``Void``, used by convention when there is no value to
return (as in a C ``void`` function).
Can be converted to an empty ``Nullable`` value.

.. data:: OS_NAME

A symbol representing the name of the operating system. Possible values
Expand Down
4 changes: 4 additions & 0 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,7 @@ for T in types
@test isa(convert(Nullable{Number}, Nullable(one(T))), Nullable{Number})
@test isa(convert(Nullable{Any}, Nullable(one(T))), Nullable{Any})
end

@test isnull(convert(Nullable, nothing))
@test isnull(convert(Nullable{Int}, nothing))
@test isa(convert(Nullable{Int}, nothing), Nullable{Int})

0 comments on commit ff3a471

Please sign in to comment.