Skip to content

Commit

Permalink
Merge branch 'master' of github.com:JuliaLang/julia
Browse files Browse the repository at this point in the history
  • Loading branch information
ViralBShah committed Dec 9, 2014
2 parents 68e5cc7 + 98865fd commit 3947393
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ end

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

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

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

function convert{S, T}(::Type{Nullable{T}}, x::Nullable{S})
return isnull(x) ? Nullable{T}() : Nullable(convert(T, get(x)))
end
Expand Down
20 changes: 18 additions & 2 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,22 @@ Dequeues

Fully implemented by: ``Vector`` (aka 1-d ``Array``), ``BitVector`` (aka 1-d ``BitArray``).

Nullables
---------

.. function:: get(x)

Attempt to access the value of the ``Nullable`` object, ``x``. Returns the
value if it is present; otherwise, throws a ``NullException``.

.. function:: get(x, y)

Attempt to access the value of the ``Nullable{T}`` object, ``x``. Returns
the value if it is present; otherwise, returns ``convert(T, y)``.

.. function:: isnull(x)

Does the ``Nullable`` object ``x`` have a value or not?

Strings
-------
Expand Down Expand Up @@ -3216,7 +3232,7 @@ Mathematical Functions
.. function:: trunc([T,] x, [digits, [base]])

``trunc(x)`` returns the nearest integral value of the same type as ``x`` whose absolute
value is less than or equal to ``x``.
value is less than or equal to ``x``.

``trunc(T, x)`` converts the result to type ``T``, throwing an
``InexactError`` if the value is not representable.
Expand All @@ -3228,7 +3244,7 @@ Mathematical Functions
``unsafe_trunc(T, x)`` returns the nearest integral value of type ``T`` whose absolute
value is less than or equal to ``x``. If the value is not representable by
``T``, an arbitrary value will be returned.

.. function:: signif(x, digits, [base])

Rounds (in the sense of ``round``) ``x`` so that there are ``digits`` significant digits, under a base ``base`` representation, default 10. E.g., ``signif(123.456, 2)`` is ``120.0``, and ``signif(357.913, 4, 2)`` is ``352.0``.
Expand Down
4 changes: 4 additions & 0 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ for T in types
x = Nullable{T}()
@test x.isnull === true
@test isa(x.value, T)
@test eltype(Nullable{T}) === T
@test eltype(x) === T
end

# Nullable{T}(value::T) = new(false, value)
Expand All @@ -27,11 +29,13 @@ for T in types
@test x.isnull === false
@test isa(x.value, T)
@test x.value === zero(T)
@test eltype(x) === T

x = Nullable{T}(one(T))
@test x.isnull === false
@test isa(x.value, T)
@test x.value === one(T)
@test eltype(x) === T
end

# immutable NullException <: Exception
Expand Down

0 comments on commit 3947393

Please sign in to comment.