【问题标题】:Eiffel: best way to compare types without getting a catcall埃菲尔:比较类型的最佳方法,而不用发出嘘声
【发布时间】:2019-06-30 03:20:11
【问题描述】:

有一个 Catcall 试图比较 2 种类型,我该如何避免通过另一个非专用方法(如字符串、class_id 或类似的东西)?

SIT_UTIL

    class_name_lowercase (a_string: STRING): STRING
            -- a copy lowercased and pruned from preceding '!'
        do
            Result := a_string
            if Result.index_of('!', 1) = 1 then
                Result := Result.substring (2, Result.count)
                Result.to_lower
            else
                Result := Result.as_lower
            end
        ensure
            instance_free: class
        end

CLIENT_CLASS

    relationship_from_secondary_type_equal (a_type: like relationships.item.secondary_type): detachable like relationships.item
            -- Returns first instance of found relationship secondary type which equals given one
        do
            across
                relationships as l_rel
            until
                Result /= Void
            loop
--              if attached (a_type / l_rel.item.secondary_type) then -- Don't want conformance but equality

--              if attached (a_type.is_equal (l_rel.item.secondary_type)) then -- tried but as is_equal needs a like Current => Catcall
--              if attached (a_type.equal (a_type, l_rel.item.secondary_type)) then -- Catcall because b signature is like a
                if {SIT_UTIL}.class_name_lowercase (a_type).is_equal({SIT_UTIL}.class_name_lowercase (l_rel.item.secondary_type)) then
                    Result := l_rel.item
                end
            end
            check
                not_found_relationship: Result /= Void
            end
        end

【问题讨论】:

  • relationships.item.secondary_type的类型是什么?
  • 对不起:TYPE[detachable S] where S -> DB_ENTITY create default_create end so TYPE[detachable DB_ENTITY]

标签: eiffel


【解决方案1】:

一致性属性是反对称的,即如果 A → BB → A,则 A = B。因此,两种类型是否相互一致:

    a_type.conforms_to (l_rel.item.secondary_type) and
    l_rel.item.secondary_type.conforms_to (a_type)

(虽然conforms_to比较的是对象的类型而不是对象本身,但上面的表达式仍然可以,因为由于一致性规则A = B当且仅当TYPE [A ] = TYPE [B]AB 本身就是类型。)

如果其中一种类型是附加的,而另一种是可拆卸的,您仍然可能希望将它们进行比较。在这种情况下,可以使用以下代码:

    is_conforming (a_type, l_rel.item.secondary_type) and
    is_conforming (l_rel.item.secondary_type, a_type)

比较谓词忽略附件标记的地方:

is_conforming (t1, t2: TYPE [detachable ANY]): BOOLEAN
  do
    Result :=
      ({REFLECTOR}.type_of_type ({REFLECTOR}.detachable_type (t1.type_id))).conforms_to
      ({REFLECTOR}.type_of_type ({REFLECTOR}.detachable_type (t2.type_id)))
  end

【讨论】:

  • 很多谢谢,现在我有一个符合类型的检查,因为!A? compared to A`没有这样做,有没有办法将一个转换为另一个或比较!insensitive_conform ;-)
  • @Pipo 我已经更新了处理不同附件状态的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多