Skip to content

Commit

Permalink
Support missing in coalesce()
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Dec 15, 2017
1 parent e0770e5 commit c4da9b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
1 change: 0 additions & 1 deletion base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ promote_rule(::Type{Missing}, ::Type{Missing}) = Missing
convert(::Type{Union{T, Missing}}, x) where {T} = convert(T, x)
# To fix ambiguities
convert(::Type{Missing}, ::Missing) = missing
convert(::Type{Void}, ::Void) = nothing
convert(::Type{Union{Void, Missing}}, x::Union{Void, Missing}) = x
convert(::Type{Union{Void, Missing}}, x) =
throw(MethodError(convert, (Union{Void, Missing}, x)))
Expand Down
12 changes: 8 additions & 4 deletions base/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@ end
"""
coalesce(x, y...)
Return the first value in the arguments which is not equal to `nothing`,
or `nothing` if all arguments are `nothing`. Unwrap arguments of type
[`Some`](@ref).
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).
# Examples
```jldoctest
julia> coalesce(nothing, 1)
1
julia> coalesce(missing, 1)
1
julia> coalesce(1, nothing)
1
Expand All @@ -63,9 +66,10 @@ function coalesce end
coalesce(x::Any) = x
coalesce(x::Some) = x.value
coalesce(x::Void) = nothing
coalesce(x::Missing) = missing
coalesce(x::Any, y...) = x
coalesce(x::Some, y...) = x.value
coalesce(x::Void, y...) = coalesce(y...)
coalesce(x::Union{Void, Missing}, y...) = coalesce(y...)

"""
notnothing(x)
Expand Down
47 changes: 27 additions & 20 deletions test/some.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,33 @@

# coalesce()

@test coalesce(1) === 1
@test coalesce(nothing) === nothing
@test coalesce(nothing, 1) === 1
@test coalesce(1, nothing) === 1
@test coalesce(nothing, nothing) === nothing
@test coalesce(nothing, 1, 2) === 1
@test coalesce(1, nothing, 2) === 1
@test coalesce(nothing, nothing, 2) === 2
@test coalesce(nothing, nothing, nothing) === nothing

@test coalesce(Some(1)) === 1
@test coalesce(Some(nothing)) === nothing
@test coalesce(Some(1), 0) === 1
@test coalesce(Some(nothing), 0) === nothing
@test coalesce(nothing, Some(nothing)) === nothing
@test coalesce(Some(1), nothing) === 1
@test coalesce(nothing, Some(1)) === 1
@test coalesce(nothing, Some(1), nothing) === 1
@test coalesce(nothing, Some(1), Some(2)) === 1
@test coalesce(Some(1), nothing, Some(2)) === 1
for v in (v, missing)
@test coalesce(1) === 1
@test coalesce(v) === v
@test coalesce(v, 1) === 1
@test coalesce(1, v) === 1
@test coalesce(v, v) === v
@test coalesce(v, 1, 2) === 1
@test coalesce(1, v, 2) === 1
@test coalesce(v, v, 2) === 2
@test coalesce(v, v, v) === v

@test coalesce(Some(1)) === 1
@test coalesce(Some(v)) === v
@test coalesce(Some(1), 0) === 1
@test coalesce(Some(v), 0) === v
@test coalesce(v, Some(v)) === v
@test coalesce(Some(1), v) === 1
@test coalesce(v, Some(1)) === 1
@test coalesce(v, Some(1), v) === 1
@test coalesce(v, Some(1), Some(2)) === 1
@test coalesce(Some(1), v, Some(2)) === 1

@test coalesce(v, missing) === missing
@test coalesce(v, nothing) === nothing
@test coalesce(v, missing, v) === v
@test coalesce(v, nothing, v) === v
end

# notnothing()

Expand Down

0 comments on commit c4da9b6

Please sign in to comment.