Eiffel:有没有办法在没有任何附加实例的情况下测试 Class 的给定通用参数?

Eiffel: Is there a way to test a given Generic parameter of a Class without any attached instance of it?

有没有办法在没有任何附加实例的情况下测试 Class 的给定通用参数?

class BAG[G -> MOUSE]

feature -- 

    discriminate
        do
            if G.conforms_to (MAMMAL) then
                io.putstring ("you gave me a real animal")
            elseif G.conforms_to (COMPUTER_ACCESSORY) then
                io.putstring ("Seems you don't like animals such as computers")
            else
                io.pustring ("Still dont know what you are dealing with")
            end
        end

您几乎成功了。缺少的部分是大括号和圆括号:

        if ({G}).conforms_to ({MAMMAL}) then
            io.put_string ("You gave me a real animal.")
        elseif ({G}).conforms_to ({COMPUTER_ACCESSORY}) then
            io.put_string ("Seems you don't like animals such as computers.")
        else
            io.put_string ("Still don't know what you are dealing with.")
        end

解释:

  1. {FOO},其中FOO是类型名,代表类型对象。它适用于任何类型,包括形式泛型,因此 {G}{MAMMAL}.
  2. 语法{FOO}.bar是为非对象调用保留的。但是这里我们想要对类型对象进行对象调用。因此,{G} 被括在括号中:({G}).conforms_to(而不是 {G}.conforms_to)。