Skip to content

Commit 7a3d9bc

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

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,25 @@ end
6161

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

64+
"""
65+
unsafe_get(x)
66+
67+
Return the value of `x` for `x::Nullable`; return `x` for all other `x`.
68+
69+
Unsafe because does not check whether or not `x` is null before attempting to
70+
access value of `x` for `x::Nullable`.
71+
"""
72+
unsafe_get(x::Nullable) = x.value
73+
unsafe_get(x) = x
74+
75+
"""
76+
isnull(x)
77+
78+
Return whether or not `x` is null for `x::Nullable`; return `false` for all
79+
other `x`.
80+
"""
6481
isnull(x::Nullable) = x.isnull
82+
isnull(x) = false
6583

6684

6785
## 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)