`==` 是否在 Julia 中递归检查结构?好像没有 [英] Does `==` for struct check recursively in Julia ? It seems not
问题描述
在检查 Julia 中有关struct"的相等性时,我不理解一种行为.对象.文档指出::对于集合,==
通常在所有内容上递归调用,但也可以考虑其他属性(如数组的形状)".虽然它似乎是结构,但它被强制转换为 ===
或其他东西.这是一个最小的工作示例:
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
=>返回真
和:
set1 = Set(["S"])
set2 = Set(["S"])
set1 == set2
=>返回真
但是!这就是我不明白的:
BUT ! And that is what I don't understand :
struct StringStruct
f::String
end
stringstruct1 = StringStruct("S")
stringstruct2 = StringStruct("S")
stringstruct1 == stringstruct2
=>返回真
但是:
struct SetStruct
f::Set{String}
end
setstruct1 = SetStruct(Set(["S"]))
setstruct2 = SetStruct(Set(["S"]))
setstruct1 == setstruct2
=>返回错误
在我看来,===
似乎在结构的元素上进行了测试.
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
s 默认 ==
回退到 ===
,例如:
For struct
s 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:
确定 x
和 y
是否相同,即没有程序可以区分它们.
Determine whether
x
andy
are identical, in the sense that no program could distinguish them.
(我省略了定义的其余部分,因为我相信这第一句话建立了一个很好的心理模型).
(I am leaving out the rest of the definition, as I believe this fist sentence builds a nice mental model).
现在显然 stringstruct1
和 stringstruct2
是不可区分的.它们是不可变的,并且包含在 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)
现在 setstrict1
和 setstruct2
是可以区分的.它们存储不同的集合,虽然在比较时这些集合包含相同的元素,但它们具有不同的内存位置(因此将来它们可以不同 - 简而言之 - 它们是可区分的).请注意,这些结构具有不同的哈希值:
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)
这一次 ss1
和 ss2
都通过了所有测试,因为它们再次无法区分(如果您更改 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屋!