簡體   English   中英

埃菲爾鐵塔:比較類型的最佳方法而不會引起注意

[英]Eiffel: best way to compare types without getting a catcall

遇到了試圖比較兩種類型的Catcall,如何避免通過另一個非專用方法(例如字符串,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

一致性屬性是反對稱的,即如果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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM