在一些 Julia 代码中,何时可以看到条件表达式,例如
if val !== nothing
dosomething()
end
where val
是一个类型变量Union{Int,Nothing}
条件有什么区别val !== nothing
and val != nothing
?
首先,一般建议使用isnothing
比较某事物是否存在nothing
。这个特定的函数非常高效,因为它完全基于类型 (@edit isnothing(nothing)
):
isnothing(::Any) = false
isnothing(::Nothing) = true
(注意nothing
是该类型的唯一实例Nothing
.)
关于你的问题,两者之间的区别===
and ==
(并且同样地!==
and !=
)是前者检查两件事是否完全相同的而后者检查equality。为了说明这种差异,请考虑以下示例:
julia> 1 == 1.0 # equal
true
julia> 1 === 1.0 # but not identical
false
请注意,前一个是整数,而后一个是浮点数。
两个事物相同意味着什么?我们可以查阅比较运算符的文档(?===
):
help?> ===
search: === == !==
===(x,y) -> Bool
≡(x,y) -> Bool
Determine whether x and y are identical, in the sense that no program could distinguish them. First the types
of x and y are compared. If those are identical, mutable objects are compared by address in memory and
immutable objects (such as numbers) are compared by contents at the bit level. This function is sometimes
called "egal". It always returns a Bool value.
有时,比较===
比比较更快==
因为后者可能涉及类型转换。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)