-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
make coalesce
handle only missing
; add something
#27258
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -695,6 +695,7 @@ export | |
ismissing, | ||
missing, | ||
skipmissing, | ||
something, | ||
|
||
# time | ||
sleep, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ showerror(io::IO, ex::MissingException) = | |
print(io, "MissingException: ", ex.msg) | ||
|
||
|
||
|
||
nonmissingtype(::Type{Union{T, Missing}}) where {T} = T | ||
nonmissingtype(::Type{Missing}) = Union{} | ||
nonmissingtype(::Type{T}) where {T} = T | ||
|
@@ -189,3 +188,30 @@ function Base.iterate(itr::SkipMissing, state...) | |
end | ||
item, state | ||
end | ||
|
||
""" | ||
coalesce(x, y...) | ||
|
||
Return the first value in the arguments which is not equal to [`missing`](@ref), | ||
if any. Otherwise return `missing`. | ||
|
||
# Examples | ||
|
||
```jldoctest | ||
julia> coalesce(missing, 1) | ||
1 | ||
|
||
julia> coalesce(1, missing) | ||
1 | ||
|
||
julia> coalesce(nothing, 1) # returns `nothing` | ||
|
||
julia> coalesce(missing, missing) | ||
missing | ||
``` | ||
""" | ||
function coalesce end | ||
|
||
coalesce() = missing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd find it more natural to define There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't ancient Rome; we understand zero now 😁 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I 100% agree with Jeff here. Zero things is missing the same way the sum of no things is zero and the product of no things is one. If we knew the type in those cases, we would absolutely define the empty sums and products to do that; in this case we do know the type and can return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, why not, though I'm not sure in what cases it could be useful. |
||
coalesce(x::Missing, y...) = coalesce(y...) | ||
coalesce(x::Any, y...) = x |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
A wrapper type used in `Union{Some{T}, Nothing}` to distinguish between the absence | ||
of a value ([`nothing`](@ref)) and the presence of a `nothing` value (i.e. `Some(nothing)`). | ||
|
||
Use [`coalesce`](@ref) to access the value wrapped by a `Some` object. | ||
Use [`something`](@ref) to access the value wrapped by a `Some` object. | ||
""" | ||
struct Some{T} | ||
value::T | ||
|
@@ -33,47 +33,39 @@ function show(io::IO, x::Some) | |
end | ||
|
||
""" | ||
coalesce(x, y...) | ||
notnothing(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As you noted, I think we can just remove |
||
|
||
Return the first value in the arguments which is not equal to | ||
either [`nothing`](@ref) or [`missing`](@ref), or the last argument. | ||
Unwrap arguments of type [`Some`](@ref). | ||
Throw an error if `x === nothing`, and return `x` if not. | ||
""" | ||
notnothing(x::Any) = x | ||
notnothing(::Nothing) = throw(ArgumentError("nothing passed to notnothing")) | ||
|
||
""" | ||
something(x, y...) | ||
|
||
Return the first value in the arguments which is not equal to [`nothing`](@ref), | ||
if any. Otherwise throw an error. | ||
Arguments of type [`Some`](@ref) are unwrapped. | ||
|
||
# Examples | ||
|
||
```jldoctest | ||
julia> coalesce(nothing, 1) | ||
julia> something(nothing, 1) | ||
1 | ||
|
||
julia> coalesce(missing, 1) | ||
julia> something(Some(1), nothing) | ||
1 | ||
|
||
julia> coalesce(1, nothing) | ||
1 | ||
julia> something(missing, nothing) | ||
missing | ||
|
||
julia> coalesce(nothing, nothing) # returns nothing, but not printed in the REPL | ||
|
||
julia> coalesce(Some(1)) | ||
1 | ||
|
||
julia> coalesce(nothing, Some(1)) | ||
1 | ||
julia> something(nothing, nothing) | ||
ERROR: ArgumentError: No value arguments present | ||
``` | ||
""" | ||
function coalesce end | ||
|
||
coalesce(x::Any) = x | ||
coalesce(x::Some) = x.value | ||
coalesce(x::Nothing) = nothing | ||
coalesce(x::Missing) = missing | ||
coalesce(x::Any, y...) = x | ||
coalesce(x::Some, y...) = x.value | ||
coalesce(x::Union{Nothing, Missing}, y...) = coalesce(y...) | ||
|
||
""" | ||
notnothing(x) | ||
function something end | ||
|
||
Throw an error if `x === nothing`, and return `x` if not. | ||
""" | ||
notnothing(x::Any) = x | ||
notnothing(::Nothing) = throw(ArgumentError("nothing passed to notnothing")) | ||
something() = throw(ArgumentError("No value arguments present")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as for the zero-argument |
||
something(x::Nothing, y...) = something(y...) | ||
something(x::Some, y...) = x.value | ||
something(x::Any, y...) = x |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably better remove this example, there's no reason to think that
nothing
is treated differently from any other value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the point of the example is to demonstrate that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I understand that, but my point is that nobody would have imagined
coalesce
would considernothing
as a missing value, except for the fact that it was the previous behavior. And since most people won't know that history...Anyway, not a big deal.