Skip to content

Commit

Permalink
Merge pull request #9351 from amitmurthy/amitm/nullable
Browse files Browse the repository at this point in the history
additional convert method for Nullable
  • Loading branch information
amitmurthy committed Dec 15, 2014
2 parents ad0722d + d25196a commit cf8e53d
Show file tree
Hide file tree
Showing 2 changed files with 14 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 @@ -19,6 +19,8 @@ function convert{T}(::Type{Nullable{T}}, x::Nullable)
return isnull(x) ? Nullable{T}() : Nullable(convert(T, get(x)))
end

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

function show{T}(io::IO, x::Nullable{T})
if x.isnull
@printf(io, "Nullable{%s}()", repr(T))
Expand Down
12 changes: 12 additions & 0 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,15 @@ for T in types
@test hash(x2) != hash(x4)
@test hash(x3) != hash(x4)
end

type TestNType{T}
v::Nullable{T}
end

for T in types
x1 = TestNType{T}(Nullable{T}())
@test isnull(x1.v)
x1.v = one(T)
@test !isnull(x1.v)
@test get(x1.v, one(T)) === one(T)
end

0 comments on commit cf8e53d

Please sign in to comment.