Missings.jl icon indicating copy to clipboard operation
Missings.jl copied to clipboard

RFC: add a `frommissing` function

Open alhirzel opened this issue 4 years ago • 1 comments

This would be a parallel to the fromMaybe function in Data.Maybe in Haskell. The definition would be sorta like this:

frommissing(default::T, value::Union{Missing, T}) where {T} = ismissing(value) ? default : value

The usage / tests could be as follows:

@test frommissing(4, missing) == 4
@test frommissing(4, 10) == 10
@test_throws MethodError frommissing(4, 10.0) # type mismatch
@test frommissing.(4, [missing, 10]) == [4, 10]

Thoughts? Would a PR be accepted?

alhirzel avatar Oct 13 '19 14:10 alhirzel

This exists in Base as coalesce(value, default).

ararslan avatar Oct 14 '19 20:10 ararslan