diff --git a/base/missing.jl b/base/missing.jl index 5c56e27216cc3..f7e04f096d489 100644 --- a/base/missing.jl +++ b/base/missing.jl @@ -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))) diff --git a/base/some.jl b/base/some.jl index 7c35d54d0e955..3229283e66e18 100644 --- a/base/some.jl +++ b/base/some.jl @@ -35,9 +35,9 @@ 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 @@ -45,6 +45,9 @@ or `nothing` if all arguments are `nothing`. Unwrap arguments of type julia> coalesce(nothing, 1) 1 +julia> coalesce(missing, 1) +1 + julia> coalesce(1, nothing) 1 @@ -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) diff --git a/test/some.jl b/test/some.jl index c4010ce8227a5..4816f791513d4 100644 --- a/test/some.jl +++ b/test/some.jl @@ -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 (nothing, 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()