双胞胎 vs deep_twin,is_equal vs is_deep_equal
twin vs deep_twin, is_equal vs is_deep_equal
twin
和 deep_twin
之间有什么区别,这意味着(并确保)埃菲尔铁塔中 is_equal
和 is_deep_equal
之间的区别?
twin
复制当前对象。如果对象引用其他对象,默认情况下它不会复制这些其他对象。这就是为什么说该函数制作 "shallow" 副本的原因。相反,deep_twin
不仅复制当前对象,而且(递归地)复制当前对象引用的所有其他对象。这是一个例子
class A create make feature
make (value: STRING) do item := value end
item: STRING
end
a: A
...
a.item = a.twin.item -- Evaluates to True
a.item = a.deep_twin.item -- Evaluates to False
在第一次比较中,twin中item
的值与原始对象中的是同一个字符串对象。第二次比较,twin中item
的值是一个新的字符串对象,等于a.item
。换句话说,如果 a.item = s
,则 a.twin.item = s
,但 a.twin.item = s1
其中 s1 /= s
,但 s1 ~ s
.
is_equal
和 is_deep_equal
是上述功能的对应部分。第一个进行 "shallow" 等式测试,第二个进行深度测试。在示例中,我们有
a.is_equal (a.twin) -- Evaluates to True
a.is_deep_equal (a.twin) -- Evaluates to True
a.is_equal (a.deep_twin) -- Evaluates to False
a.is_deep_equal (a.deep_twin) -- Evaluates to True
如果对象 "shallow" 相等,则它们也 "deep" 相等。但反之则不然。
假设使用 is_equal
和 twin
的默认版本,以上所有内容均有效。可以重新定义这些功能以更深入。对于上面的 class 我们可以添加
is_equal (other: like Current): BOOLEAN
do
-- The default version is "Result := item = other.item"
Result := item ~ other.item -- Compare content rather than references
end
copy (other: like Current) -- twin is defined in terms of this procedure
do
-- The default version is "item := other.item"
item := other.item.twin -- Use a copy instead of the reference
end
通过这个重新定义,twin
/deep_twin
和is_equal
/is_deep_equal
变得几乎一样了。
twin
和 deep_twin
之间有什么区别,这意味着(并确保)埃菲尔铁塔中 is_equal
和 is_deep_equal
之间的区别?
twin
复制当前对象。如果对象引用其他对象,默认情况下它不会复制这些其他对象。这就是为什么说该函数制作 "shallow" 副本的原因。相反,deep_twin
不仅复制当前对象,而且(递归地)复制当前对象引用的所有其他对象。这是一个例子
class A create make feature
make (value: STRING) do item := value end
item: STRING
end
a: A
...
a.item = a.twin.item -- Evaluates to True
a.item = a.deep_twin.item -- Evaluates to False
在第一次比较中,twin中item
的值与原始对象中的是同一个字符串对象。第二次比较,twin中item
的值是一个新的字符串对象,等于a.item
。换句话说,如果 a.item = s
,则 a.twin.item = s
,但 a.twin.item = s1
其中 s1 /= s
,但 s1 ~ s
.
is_equal
和 is_deep_equal
是上述功能的对应部分。第一个进行 "shallow" 等式测试,第二个进行深度测试。在示例中,我们有
a.is_equal (a.twin) -- Evaluates to True
a.is_deep_equal (a.twin) -- Evaluates to True
a.is_equal (a.deep_twin) -- Evaluates to False
a.is_deep_equal (a.deep_twin) -- Evaluates to True
如果对象 "shallow" 相等,则它们也 "deep" 相等。但反之则不然。
假设使用 is_equal
和 twin
的默认版本,以上所有内容均有效。可以重新定义这些功能以更深入。对于上面的 class 我们可以添加
is_equal (other: like Current): BOOLEAN
do
-- The default version is "Result := item = other.item"
Result := item ~ other.item -- Compare content rather than references
end
copy (other: like Current) -- twin is defined in terms of this procedure
do
-- The default version is "item := other.item"
item := other.item.twin -- Use a copy instead of the reference
end
通过这个重新定义,twin
/deep_twin
和is_equal
/is_deep_equal
变得几乎一样了。