Skip to content

Commit

Permalink
Add generic isnull, unsafe_get methods
Browse files Browse the repository at this point in the history
Also move isnull docstring out of helpdb and into base/nullable.jl.
  • Loading branch information
davidagold committed Sep 29, 2016
1 parent b36141f commit ef546f5
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
7 changes: 0 additions & 7 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1466,13 +1466,6 @@ julia> log2(10)
"""
log2

"""
isnull(x)
Is the `Nullable` object `x` null, i.e. missing a value?
"""
isnull

"""
abs2(x)
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ export

# nullable types
isnull,
unsafe_get,

# Macros
# parser internal
Expand Down
62 changes: 62 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,68 @@ end

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

"""
unsafe_get(x)
Return the value of `x` for [`Nullable`](:obj:`Nullable`) `x`; return `x` for
all other `x`.
This method does not check whether or not `x` is null before attempting to
access the value of `x` for `x::Nullable` (hence "unsafe").
```jldoctest
julia> x = Nullable(1)
Nullable{Int64}(1)
julia> unsafe_get(x)
1
julia> x = Nullable{String}()
Nullable{String}()
julia> unsafe_get(x)
ERROR: UndefRefError: access to undefined reference
in unsafe_get(::Nullable{String}) at ./REPL[4]:1
julia> x = 1
1
julia> unsafe_get(x)
1
```
"""
unsafe_get(x::Nullable) = x.value
unsafe_get(x) = x

"""
isnull(x)
Return whether or not `x` is null for [`Nullable`](:obj:`Nullable`) `x`; return
`false` for all other `x`.
```jldoctest
julia> x = Nullable(1, false)
Nullable{Int64}(1)
julia> isnull(x)
false
julia> x = Nullable(1, true)
Nullable{Int64}()
julia> isnull(x)
true
julia> x = 1
1
julia> isnull(x)
false
```
"""
isnull(x::Nullable) = x.isnull
isnull(x) = false

isnull(x::Nullable) = !x.hasvalue

## Operators
Expand Down
38 changes: 37 additions & 1 deletion test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,51 @@ for T in types
@test get(x3, zero(T)) === one(T)
end

# isnull(x::Nullable)
for T in types
# unsafe_get(x::Nullable)
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
a = rand(T)
x4 = Nullable(a)

@test isa(unsafe_get(x1), T)
@test unsafe_get(x2) === zero(T)
@test unsafe_get(x3) === one(T)
@test unsafe_get(x4) === a

# unsafe_get(x)
x2 = zero(T)
x3 = one(T)
x4 = rand(T)

@test unsafe_get(x2) === zero(T)
@test unsafe_get(x3) === one(T)
@test unsafe_get(x4) === x4
end

@test_throws UndefRefError unsafe_get(Nullable())
@test_throws UndefRefError unsafe_get(Nullable{String}())
@test_throws UndefRefError unsafe_get(Nullable{Array}())

for T in types
# isnull(x::Nullable)
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))

@test isnull(x1) === true
@test isnull(x2) === false
@test isnull(x3) === false

# isnull(x)
x1 = zero(T)
x2 = one(T)
x3 = rand(T)

@test isnull(x1) === false
@test isnull(x2) === false
@test isnull(x3) === false
end

@test isnull(Nullable())
Expand Down

0 comments on commit ef546f5

Please sign in to comment.