Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic isnull and unsafe_get methods #18484

Merged
merged 1 commit into from
Oct 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1466,13 +1466,6 @@ julia> log2(10)
"""
log2

"""
isnull(x)

Is the `Nullable` object `x` null, i.e. missing a value?
"""
isnull

"""
abs2(x)

Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,7 @@ export

# nullable types
isnull,
unsafe_get,

# Macros
# parser internal
Expand Down
62 changes: 62 additions & 0 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,68 @@ end

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

"""
unsafe_get(x)
Return the value of `x` for [`Nullable`](:obj:`Nullable`) `x`; return `x` for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just being curious, do we need this to get links?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's my understanding. @kshyatt included these in https://github.com/JuliaLang/julia/pull/18511/files#diff-1955b2684af6e5a407aa36281f8bafdeR94, so I figured I'd include them here, too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this documented somewhere? Maybe somebody could add a word about this at http://docs.julialang.org/en/latest/manual/documentation/? @MichaelHatherly

(FWIW, the repetition of the object name sounds a bit annoying to me...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That syntax is being removed in #18588 in favour of [Nullable](@ref), so probably no need to add it to the docs at this point.

so I figured I'd include them here, too.

We can never have too many cross references 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll remove the references for now. I think we will all live with the repetition in the docstring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the auto conversion script translate them from the current rst-in-markdown hybrid to the new style? if so it won't hurt to add if this gets merged before the conversion

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, handled automatically. It's fine to keep adding xrefs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will add it back in.

all other `x`.
This method does not check whether or not `x` is null before attempting to
access the value of `x` for `x::Nullable` (hence "unsafe").
```jldoctest
julia> x = Nullable(1)
Nullable{Int64}(1)
julia> unsafe_get(x)
1
julia> x = Nullable{String}()
Nullable{String}()
julia> unsafe_get(x)
ERROR: UndefRefError: access to undefined reference
in unsafe_get(::Nullable{String}) at ./REPL[4]:1
julia> x = 1
1
julia> unsafe_get(x)
1
```
"""
unsafe_get(x::Nullable) = x.value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a docstring.

unsafe_get(x) = x

"""
isnull(x)
Return whether or not `x` is null for [`Nullable`](:obj:`Nullable`) `x`; return
`false` for all other `x`.
```jldoctest
julia> x = Nullable(1, false)
Nullable{Int64}(1)
julia> isnull(x)
false
julia> x = Nullable(1, true)
Nullable{Int64}()
julia> isnull(x)
true
julia> x = 1
1
julia> isnull(x)
false
```
"""
isnull(x::Nullable) = x.isnull
isnull(x) = false

isnull(x::Nullable) = !x.hasvalue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad rebase


## Operators
Expand Down
38 changes: 37 additions & 1 deletion test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,51 @@ for T in types
@test get(x3, zero(T)) === one(T)
end

# isnull(x::Nullable)
for T in types
# unsafe_get(x::Nullable)
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))
a = rand(T)
x4 = Nullable(a)

@test isa(unsafe_get(x1), T)
@test unsafe_get(x2) === zero(T)
@test unsafe_get(x3) === one(T)
@test unsafe_get(x4) === a

# unsafe_get(x)
x2 = zero(T)
x3 = one(T)
x4 = rand(T)

@test unsafe_get(x2) === zero(T)
@test unsafe_get(x3) === one(T)
@test unsafe_get(x4) === x4
end

@test_throws UndefRefError unsafe_get(Nullable())
@test_throws UndefRefError unsafe_get(Nullable{String}())
@test_throws UndefRefError unsafe_get(Nullable{Array}())

for T in types
# isnull(x::Nullable)
x1 = Nullable{T}()
x2 = Nullable(zero(T))
x3 = Nullable(one(T))

@test isnull(x1) === true
@test isnull(x2) === false
@test isnull(x3) === false

# isnull(x)
x1 = zero(T)
x2 = one(T)
x3 = rand(T)

@test isnull(x1) === false
@test isnull(x2) === false
@test isnull(x3) === false
end

@test isnull(Nullable())
Expand Down