Eiffel:没有字段的扩展 类 是否为 `=`?

Eiffel: Expanded classes with no fields are `=` or not?

在 Eiffel 中,如果比较扩展类型的对象,= 运算符会逐字段比较它们,检查两个对象中每个字段的内容是否相同。

让我们想象两个没有定义特征的扩展 类:

expanded class A
end

expanded class B
end

Eiffel 如何区分它们?还是不能?它是否与从 ANY 继承的某些字段有关?

both_are_equal: BOOLEAN
    local
        a: expanded A
        b: expanded B
    do
        Result := a = b
    end

仅当两个对象的类型相同时才应用逐字段比较。如果它们属于不同类型,则相等运算符给出 false。换句话说,扩展类型的相等运算符 = 与具有语义

的相等运算符 ~ 相同
type_of (a) = type_of (b) and then a.is_equal (b)

因此both_are_equal会给出False.

如果不是 ab 扩展类型,而是附加到扩展对象的 xy 引用类型,结果将是相同的 -比较考虑了对象类型:

both_are_equal: BOOLEAN
    local
        a: expanded A
        b: expanded B
        x: ANY
        y: ANY
    do
        x := a
        y := b
        Result := x = y -- Gives False
    end

但如果引用表达式附加到相同类型的扩展对象,则使用逐字段比较,而不是引用相等:

both_are_equal: BOOLEAN
    local
        a: expanded A
        b: expanded A -- Note the type change
        x: ANY
        y: ANY
    do
        x := a
        y := b
        Result := x = y -- Gives True even though x and y are different objects
    end

可以在 Standard ECMA-367 (section 8.21) and in contracts of specific comparison features in the class ANY.

中找到有关相等运算符的一些详细信息