Is there a type subtraction operation in Julia?

2019-08-29 05:41发布

In Julia we have typeintersect(Missing, Union{Missing, Float64}) (returning Missing). Is it possible to get what remains instead (i.e. Union{Missing, Float64} - Missing returning Float64)?

I did try with typesubtract(Missing, Union{Missing, Float64}) or typecomplement(Union{Missing, Float64}, Missing) but obviously they don't exist ;-)

标签: julia
1条回答
我命由我不由天
2楼-- · 2019-08-29 05:58

For Missing it is actually implemented in Base (but not exported) as nonmissingtype function. Here you have the relevant code:

nonmissingtype(::Type{Union{T, Missing}}) where {T} = T
nonmissingtype(::Type{Missing}) = Union{}
nonmissingtype(::Type{T}) where {T} = T
nonmissingtype(::Type{Any}) = Any

So this should probably solve your issue with Missing (just import this function from Base) and you have a template how a similar thing can be implemented for other scenarios. Please let me know if it answers what you wanted.

查看更多
登录 后发表回答