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 @? macro #136

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 21 additions & 8 deletions src/Missings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Missings

export allowmissing, disallowmissing, ismissing, missing, missings,
Missing, MissingException, levels, coalesce, passmissing, nonmissingtype,
skipmissings
skipmissings, @?

using Base: ismissing, missing, Missing, MissingException

Expand Down Expand Up @@ -283,7 +283,7 @@ struct SkipMissings{V, T}
others::T
end

Base.@propagate_inbounds function _anymissingindex(others::Tuple{Vararg{AbstractArray}}, i)
Base.@propagate_inbounds function _anymissingindex(others::Tuple{Vararg{AbstractArray}}, i)
for oth in others
oth[i] === missing && return true
end
Expand All @@ -292,7 +292,7 @@ Base.@propagate_inbounds function _anymissingindex(others::Tuple{Vararg{Abstract
end

@inline function _anymissingiterate(others::Tuple, state)
for oth in others
for oth in others
y = iterate(oth, state)
y !== nothing && first(y) === missing && return true
end
Expand All @@ -303,7 +303,7 @@ end
const SkipMissingsofArrays = SkipMissings{V, T} where
{V <: AbstractArray, T <: Tuple{Vararg{AbstractArray}}}

function Base.show(io::IO, mime::MIME"text/plain", itr::SkipMissings{V}) where V
function Base.show(io::IO, mime::MIME"text/plain", itr::SkipMissings{V}) where V
print(io, SkipMissings, '{', V, '}', '(', itr.x, ')', " comprised of " *
"$(length(itr.others) + 1) iterators")
end
Expand Down Expand Up @@ -360,7 +360,7 @@ end
@inline function Base.getindex(itr::SkipMissingsofArrays, i)
@boundscheck checkbounds(itr.x, i)
@inbounds xi = itr.x[i]
if xi === missing || @inbounds _anymissingindex(itr.others, i)
if xi === missing || @inbounds _anymissingindex(itr.others, i)
throw(MissingException("the value at index $i is missing for some element"))
end
return xi
Expand Down Expand Up @@ -405,9 +405,9 @@ Base.mapreduce_impl(f, op, A::SkipMissingsofArrays, ifirst::Integer, ilast::Inte
A = itr.x
if ifirst == ilast
@inbounds a1 = A[ifirst]
if a1 === missing
if a1 === missing
return nothing
elseif _anymissingindex(itr.others, ifirst)
elseif _anymissingindex(itr.others, ifirst)
return nothing
else
return Some(Base.mapreduce_first(f, op, a1))
Expand Down Expand Up @@ -461,7 +461,7 @@ end
Return a vector similar to the array wrapped by the given `SkipMissings` iterator
but skipping all elements with a `missing` value in one of the iterators passed
to `skipmissing` and elements for which `f` returns `false`. This method
only applies when all iterators passed to `skipmissings` are arrays.
only applies when all iterators passed to `skipmissings` are arrays.

# Examples
```
Expand Down Expand Up @@ -490,4 +490,17 @@ function filter(f, itr::SkipMissingsofArrays)
y
end

if VERSION >= v"1.3"
include("unionmissing.jl")
else
union_macro_ex = :(
macro _(typ)
:(Union{$(esc(typ)), Missing})
end
)

union_macro_ex.args[1].args[1] = :?
eval(union_macro_ex)
end

end # module
28 changes: 28 additions & 0 deletions src/unionmissing.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
@?(ex)

Wrap an expression `ex` into `Union{Missing, ex}`.

Note that Julia will pass a whole expression `ex` following `@? to it`.
Therefore using parantheses to correctly specify the `ex` is sometimes required.

```jldoctest
julia> Vector{@?Int}
Vector{Union{Missing, Int64}} (alias for Array{Union{Missing, Int64}, 1})

julia> Union{String, @?Int}
Union{Missing, Int64, String}

julia> Union{@?(Int), String}
Union{Missing, Int64, String}

julia> @?(Int)[1, 2, 3]
3-element Vector{Union{Missing, Int64}}:
1
2
3
```
"""
macro var"?"(typ)
:(Union{$(esc(typ)), Missing})
end
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ struct CubeRooter end
@test passmissing(Int) === Missings.PassMissing{Type{Int}}(Int)
@test passmissing(cuberoot) === Missings.PassMissing{CubeRooter}(cuberoot)

@testset "@?" begin
@test (@?Int) === Union{Int, Missing}
@test @?(Int)[] isa Vector{Union{Int, Missing}}
end

@testset "deprecated" begin
x = [1, 2, missing, 4]
y = ["a", "b", "c", missing]
Expand Down