Skip to content

Commit

Permalink
New convert method for non-Nullables to Nullables
Browse files Browse the repository at this point in the history
  • Loading branch information
iamed2 committed Oct 15, 2015
1 parent cb02c00 commit 990a14c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ eltype{T}(::Type{Nullable{T}}) = T

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

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

function convert{T}(::Type{Nullable{T}}, x::Nullable)
return isnull(x) ? Nullable{T}() : Nullable{T}(convert(T, get(x)))
end
Expand Down
9 changes: 9 additions & 0 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,16 @@ end
# issue #9462
for T in types
@test isa(convert(Nullable{Number}, Nullable(one(T))), Nullable{Number})
@test isa(convert(Nullable{Number}, one(T)), Nullable{Number})
@test isa(convert(Nullable{T}, one(T)), Nullable{T})
@test isa(convert(Nullable{Any}, Nullable(one(T))), Nullable{Any})
@test isa(convert(Nullable{Any}, one(T)), Nullable{Any})

# one(T) is convertible to every type in types
# let's test that with Nullables
for S in types
@test isa(convert(Nullable{T}, one(S)), Nullable{T})
end
end

@test isnull(convert(Nullable, nothing))
Expand Down

0 comments on commit 990a14c

Please sign in to comment.