Skip to content

Commit

Permalink
Add generic isnull, unsafe_get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davidagold committed Sep 28, 2016
1 parent 8d2fac8 commit 9356b27
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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
4 changes: 4 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ end

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

unsafe_get(x::Nullable) = x.value
unsafe_get(x) = x

isnull(x::Nullable) = x.isnull
isnull(x) = false


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

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

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

@test_throws UndefRefError unsafe_get(Nullable())

# unsafe_get(x)
for T in types
x1 = zero(T)
x2 = one(T)
x3 = rand(T)

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

# isnull(x::Nullable)
for T in types
x1 = Nullable{T}()
Expand All @@ -174,6 +201,17 @@ end

@test isnull(Nullable())

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

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

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

0 comments on commit 9356b27

Please sign in to comment.