So I was looking at how to get T
from a Union{Missing,T}
and came up with
nonmissing(TT::Type{Union{Missing,T}}) where T = T
nonmissing(TT::Type{T}) where T = T
(PS: incidentally I found that @bkamins had answered something similar on stackoverflow Is there a type subtraction operation in Julia? - Stack Overflow so it looks like this is not a bad idea)
This works fine for non-parametric types including abstract types:
julia> nonmissing(Union{Missing,Real}) == Real
However when there’s a parametric type, it returns the union
julia> struct Foo{N} end
julia> nonmissing(Union{Missing,Foo}) == Union{Missing,Foo}
Is there a way to recuperate Foo
in this case? One way I found around it that works is to write a macro but I feel that’s overkill here…
macro nonmissing(ex)
ex isa Symbol && return ex
types = ex.args[2:end]
types[1] == :Missing && return types[2]
return types[1]
end
thanks