本文介绍了使用 !== 或 != 将 Julia 变量与 `nothing` 进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在一些Julia代码中什么时候可以看到条件表达式如

In some Julia code when can see conditional expression such as

if val !== nothing
    dosomething()
end

其中 valUnion{Int,Nothing}

条件 val !== nothingval != nothing 有什么区别?

What is the difference between conditons val !== nothing and val != nothing?

推荐答案

首先,一般建议使用isnothing来比较某事物是否为nothing.这个特殊的函数很有效,因为它是基于类型的(@edit isnothing(nothing)):

First of all, it is generally advisable to use isnothing to compare if something is nothing. This particular function is efficient, as it is soley based on types (@edit isnothing(nothing)):

isnothing(::Any) = false
isnothing(::Nothing) = true

(请注意,nothingNothing 类型的唯一实例.)

(Note that nothing is the only instance of the type Nothing.)

关于您的问题,===== 之间的区别(同样 !==!=) 是前者检查两个事物是否相同,而后者检查相等.为了说明这种差异,请考虑以下示例:

In regards to your question, the difference between === and == (and equally !== and !=) is that the former checks whether two things are identical whereas the latter checks for equality. To illustrate this difference, consider the following example:

julia> 1 == 1.0 # equal
true

julia> 1 === 1.0 # but not identical
false

注意,前一个是整数,后一个是浮点数.

Note that the former one is an integer whereas the latter one is a floating point number.

两个事物相同是什么意思?我们可以查阅比较运算符的文档(?===):

What does it mean for two things to be identical? We can consult the documentation of the comparison operators (?===):

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.

有时,与 === 比较比与 == 比较更快,因为后者可能涉及类型转换.

Sometimes, comparing with === is faster than comparing with == because the latter might involve a type conversion.

这篇关于使用 !== 或 != 将 Julia 变量与 `nothing` 进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 12:22