[英]Eiffel: Is there a way to test a given Generic parameter of a Class without any attached instance of it?
Is there a way to test a given Generic parameter of a Class without any attached instance of it? 有没有办法在没有任何附加实例的情况下测试类的给定泛型参数?
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
You've almost nailed it. 您几乎钉死了它。 The missing part is curly braces and parentheses:
缺少的部分是花括号和括号:
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
Explanation: 说明:
{FOO}
, where FOO
is a type name, stands for a type object. {FOO}
(其中FOO
是类型名称)代表类型对象。 It works for any type, including formal generics, thus {G}
and {MAMMAL}
. {G}
和{MAMMAL}
。 {FOO}.bar
is reserved for non-object calls. {FOO}.bar
保留用于非对象调用。 But here we want an object call on the type object. {G}
is enclosed in parentheses: ({G}).conforms_to
(instead of {G}.conforms_to
). {G}
括在括号中: ({G}).conforms_to
{G}.conforms_to
(而不是{G}.conforms_to
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.