是否在Julia中递归检查结构的==`?好像没有 [英] Does `==` for struct check recursively in Julia ? It seems not

查看:54
本文介绍了是否在Julia中递归检查结构的==`?好像没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我检查Julia中有关结构"的相等性时,有一种我不理解的行为.对象. 文档指出:对于集合,==通常在所有内容上递归调用==,尽管也可以考虑其他属性(例如数组的形状)".尽管对于结构而言,它似乎已强制转换为===之类的东西. 这是一个最小的工作示例:

There is a behavior I don't understand when checking for equality in Julia concerning "struct" objects. The documentation states : : "For collections, == is generally called recursively on all contents, though other properties (like the shape for arrays) may also be taken into account". Though it seems for structs it is cast to === or something. Here is a minimal working exemple :

符合预期:

string1 = String("S")
string2 = String("S")
string1 == string2 

=>返回true

=> returns true

和:

set1 = Set(["S"])
set2 = Set(["S"])
set1 == set2

=>返回true

=> returns true

但是!那就是我不明白的:

BUT ! And that is what I don't understand :

struct StringStruct
    f::String
end
stringstruct1 = StringStruct("S")  
stringstruct2 = StringStruct("S")  
stringstruct1 == stringstruct2  

=>返回true

=> returns true

但是:

struct SetStruct  
    f::Set{String}  
end
setstruct1 = SetStruct(Set(["S"]))
setstruct2 = SetStruct(Set(["S"]))
setstruct1 == setstruct2 

=>返回false

=> returns false

在我看来,===在结构的元素上进行了测试.

To me, it looks like ===is tested on the elements of the struct.

所以我的问题是:在结构上进行测试时,==的真实行为是什么?它是否转换=====?如果文件说明中注明==,我误解了什么?

So my question is : what is the real behavior of == when tested on structs ? Does it cast == or === ? In case it casts == as the documentation states, what is the point I misunderstand ?

推荐答案

对于struct而言,默认情况下==会退回到===,因此例如:

For structs by default == falls back to ===, so e.g.:

setstruct1 == setstruct2

setstruct1 === setstruct2

因此,现在我们来看一下===的工作方式.它定义为:

So now we go down to the way how === works. And it is defined as:

在没有程序可以区分它们的意义上,确定xy是否相同.

(我遗漏了其余的定义,因为我相信这第一句话造就了一个不错的心理模型).

(I am leaving out the rest of the definition, as I believe this fist sentence builds a nice mental model).

现在显然stringstruct1stringstruct2是不可区分的.它们是不可变的,并且包含在Julia中不可变的字符串.特别是它们具有相同的hash值(这不是确定的测试,但在这里是一个很好的心理模型).

Now clearly stringstruct1 and stringstruct2 are non-distinguishable. They are immutable and contain strings that are immutable in Julia. In particular they have the same hash value (which is not a definitive test, but is a nice mental model here).

julia> hash(stringstruct1), hash(stringstruct2)
(0x9e0bef39ad32ce56, 0x9e0bef39ad32ce56)

现在setstrict1setstruct2是可区分的.它们存储不同的集合,尽管在比较时这些集合包含相同的元素,但是它们具有不同的存储位置(因此将来它们可能会有所不同-简而言之-它们是可区分的).请注意,这些结构尤其具有不同的哈希值:

Now setstrict1 and setstruct2 are distinguishable. They store different sets, although at the moment of comparison these sets contain the same elements, but they have different memory locations (so in the future they can be different - in short - they are distinguishable). Note that these structs have different hashes in particular:

julia> hash(setstruct1), hash(setstruct2)
(0xe7d0f90913646f29, 0x3b31ce0af9245c64)

现在注意以下几点:

julia> s = Set(["S"])
Set{String} with 1 element:
  "S"

julia> ss1 = SetStruct(s)
SetStruct(Set(["S"]))

julia> ss2 = SetStruct(s)
SetStruct(Set(["S"]))

julia> ss1 == ss2
true

julia> ss1 === ss2
true

julia> hash(ss1), hash(ss2)
(0x9127f7b72f753361, 0x9127f7b72f753361)

这次ss1ss2通过了所有测试,因为它们又无法区分(如果更改ss1,则ss2会同步更改,因为它们保持相同的Set).

This time ss1 and ss2 are passing all the tests, as again they are indistinguishable (if you change ss1 then ss2 changes in sync as they hold the same Set).

这篇关于是否在Julia中递归检查结构的==`?好像没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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