如何在 Eiffel 中测试正式泛型类型的对象是否相等?

How to test objects of formal generic types for equality in Eiffel?

我应该测试两个通用属性是否相等,例如

a, b: G
...
Result := a.is_equal (b)

但是 VUTA(目标规则有效性)表示“Object_call 的单独目标不受控制”。我能做什么?

看来你去看医生的时候...

Result := attached a as x and then attached b as y and then y.is_equal (x)

...亚历山大感谢您的编辑

以下是解决此问题的不同选项,它们仅依赖于您的代码更改:

  1. 为形式泛型参数添加非单独约束。默认情况下,约束为 detachable separate ANY。例如,如果您有声明

    class FOO [G] ...
    

    然后指定显式约束 ANY(没有 separate)应该可以解决问题:

    class FOO [G -> ANY] ...
    

    通过此更改,类型 G 的所有实例也将被附加。如果要避免这种情况,约束 ANY 应替换为 detachable ANY,但应更新代码以在使用前检查属性是否已附加:

    Result := attached a as x and then attached b as y and then x.is_equal (y)
    
  2. 包装调用的目标以使其受控。这是带有单独指令的示例(可以使用专用功能编写类似的代码,例如,如果您有许多此类相等性测试):

    separate a as x do
        Result := attached x and then attached b as y and then x.is_equal (b)
    end
    

如果不需要,也可以更改项目设置以避免使用 SCOOP。