Skip to content

Commit 0501d36

Browse files
Merge pull request #11576 from JuliaLang/sb/nullable
add 2 argument Nullable constructor
2 parents a6fe623 + fae0b8c commit 0501d36

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

base/base.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,5 @@ immutable Nullable{T}
105105
value::T
106106

107107
Nullable() = new(true)
108-
Nullable(value::T) = new(false, value)
108+
Nullable(value::T, isnull::Bool=false) = new(isnull, value)
109109
end

base/nullable.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
immutable NullException <: Exception
44
end
55

6-
Nullable{T}(value::T) = Nullable{T}(value)
6+
Nullable{T}(value::T, isnull::Bool=false) = Nullable{T}(value, isnull)
77
Nullable() = Nullable{Union()}()
88

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

test/nullable.jl

+17
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ for T in types
4040
@test eltype(x) === T
4141
end
4242

43+
# Nullable{T}(value::T, isnull::Bool) = new(isnull, value)
44+
for T in types
45+
x = Nullable{T}(zero(T),false)
46+
@test x.isnull === false
47+
@test isa(x.value, T)
48+
@test x.value === zero(T)
49+
@test eltype(x) === T
50+
51+
x = Nullable{T}(zero(T),true)
52+
@test x.isnull === true
53+
@test isa(x.value, T)
54+
@test eltype(Nullable{T}) === T
55+
@test eltype(x) === T
56+
end
57+
58+
59+
4360
# immutable NullException <: Exception
4461
@test isa(NullException(), NullException)
4562
@test_throws NullException throw(NullException())

0 commit comments

Comments
 (0)