Skip to content

Commit ef546f5

Browse files
committed
Add generic isnull, unsafe_get methods
Also move isnull docstring out of helpdb and into base/nullable.jl.
1 parent b36141f commit ef546f5

File tree

4 files changed

+100
-8
lines changed

4 files changed

+100
-8
lines changed

base/docs/helpdb/Base.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,13 +1466,6 @@ julia> log2(10)
14661466
"""
14671467
log2
14681468

1469-
"""
1470-
isnull(x)
1471-
1472-
Is the `Nullable` object `x` null, i.e. missing a value?
1473-
"""
1474-
isnull
1475-
14761469
"""
14771470
abs2(x)
14781471

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,6 +1324,7 @@ export
13241324

13251325
# nullable types
13261326
isnull,
1327+
unsafe_get,
13271328

13281329
# Macros
13291330
# parser internal

base/nullable.jl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,68 @@ end
6161

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

64+
"""
65+
unsafe_get(x)
66+
67+
Return the value of `x` for [`Nullable`](:obj:`Nullable`) `x`; return `x` for
68+
all other `x`.
69+
70+
This method does not check whether or not `x` is null before attempting to
71+
access the value of `x` for `x::Nullable` (hence "unsafe").
72+
73+
```jldoctest
74+
julia> x = Nullable(1)
75+
Nullable{Int64}(1)
76+
77+
julia> unsafe_get(x)
78+
1
79+
80+
julia> x = Nullable{String}()
81+
Nullable{String}()
82+
83+
julia> unsafe_get(x)
84+
ERROR: UndefRefError: access to undefined reference
85+
in unsafe_get(::Nullable{String}) at ./REPL[4]:1
86+
87+
julia> x = 1
88+
1
89+
90+
julia> unsafe_get(x)
91+
1
92+
```
93+
"""
94+
unsafe_get(x::Nullable) = x.value
95+
unsafe_get(x) = x
96+
97+
"""
98+
isnull(x)
99+
100+
Return whether or not `x` is null for [`Nullable`](:obj:`Nullable`) `x`; return
101+
`false` for all other `x`.
102+
103+
```jldoctest
104+
julia> x = Nullable(1, false)
105+
Nullable{Int64}(1)
106+
107+
julia> isnull(x)
108+
false
109+
110+
julia> x = Nullable(1, true)
111+
Nullable{Int64}()
112+
113+
julia> isnull(x)
114+
true
115+
116+
julia> x = 1
117+
1
118+
119+
julia> isnull(x)
120+
false
121+
```
122+
"""
123+
isnull(x::Nullable) = x.isnull
124+
isnull(x) = false
125+
64126
isnull(x::Nullable) = !x.hasvalue
65127

66128
## Operators

test/nullable.jl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,51 @@ for T in types
161161
@test get(x3, zero(T)) === one(T)
162162
end
163163

164-
# isnull(x::Nullable)
165164
for T in types
165+
# unsafe_get(x::Nullable)
166+
x1 = Nullable{T}()
167+
x2 = Nullable(zero(T))
168+
x3 = Nullable(one(T))
169+
a = rand(T)
170+
x4 = Nullable(a)
171+
172+
@test isa(unsafe_get(x1), T)
173+
@test unsafe_get(x2) === zero(T)
174+
@test unsafe_get(x3) === one(T)
175+
@test unsafe_get(x4) === a
176+
177+
# unsafe_get(x)
178+
x2 = zero(T)
179+
x3 = one(T)
180+
x4 = rand(T)
181+
182+
@test unsafe_get(x2) === zero(T)
183+
@test unsafe_get(x3) === one(T)
184+
@test unsafe_get(x4) === x4
185+
end
186+
187+
@test_throws UndefRefError unsafe_get(Nullable())
188+
@test_throws UndefRefError unsafe_get(Nullable{String}())
189+
@test_throws UndefRefError unsafe_get(Nullable{Array}())
190+
191+
for T in types
192+
# isnull(x::Nullable)
166193
x1 = Nullable{T}()
167194
x2 = Nullable(zero(T))
168195
x3 = Nullable(one(T))
169196

170197
@test isnull(x1) === true
171198
@test isnull(x2) === false
172199
@test isnull(x3) === false
200+
201+
# isnull(x)
202+
x1 = zero(T)
203+
x2 = one(T)
204+
x3 = rand(T)
205+
206+
@test isnull(x1) === false
207+
@test isnull(x2) === false
208+
@test isnull(x3) === false
173209
end
174210

175211
@test isnull(Nullable())

0 commit comments

Comments
 (0)