Julia中有类型减法运算吗? [英] Is there a type subtraction operation in Julia?

查看:79
本文介绍了Julia中有类型减法运算吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Julia中,我们有typeintersect(Missing, Union{Missing, Float64})(返回Missing).

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)?

我确实尝试过typesubtract(Missing, Union{Missing, Float64})typecomplement(Union{Missing, Float64}, Missing),但显然它们不存在;-)

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

推荐答案

对于Missing,它实际上在Base中实现(但未导出)为nonmissingtype函数.这里您有相关的代码:

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

因此,这可能应该可以解决Missing(只是import来自Base的此功能)的问题,并且您有一个模板可以针对其他情况实现类似的功能.请让我知道它是否满足您的要求.

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.

这篇关于Julia中有类型减法运算吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆