Eiffel:一种检查类型与给定 CLASS_NAME 一致性的方法
Eiffel: a way to check type conformance with a given CLASS_NAME
我正在尝试做一些事情
work (a_father: FATHER)
do
if a_father.conforms_to ({DEVELOPER}) then
a_father.code
else
a_father.change_job ({DEVELOPER})
end
end
编译有效,但在我的@runtime 实现中它没有通过。我输错了什么?
您的示例中的问题是您试图查看类型 FATHER
(对象的类型 a_father
)是否符合类型 TYPE [DEVELOPER]
(对象的类型对象 {DEVELOPER}
).
你应该做的是:
if a_father.generating_type.is_conforming_to ({DEVELOPER}) then
因此将 TYPE [FATHER]
与 TYPE [DEVELOPER]
进行比较。
请注意,我假设它可以通过将 is_conforming_to
替换为 conforms_to
来工作,但是 class TYPE
引入了这个例程 is_conforming_to
更多具体参数类型。
我最好使用 built-in 机制来检查对象类型一致性:
if attached {DEVELOPER} a_father as dev then
dev.code
else
a_father.rest
end
并在前提条件中使用相同的方法:
attached {RELATED_DB_ENTITY} a_relationship_entity
对象测试做你想做的事:它检查附加到参数 a_relationship_entity
的对象类型是否符合类型 RELATED_DB_ENTITY
.
我正在尝试做一些事情
work (a_father: FATHER)
do
if a_father.conforms_to ({DEVELOPER}) then
a_father.code
else
a_father.change_job ({DEVELOPER})
end
end
编译有效,但在我的@runtime 实现中它没有通过。我输错了什么?
您的示例中的问题是您试图查看类型 FATHER
(对象的类型 a_father
)是否符合类型 TYPE [DEVELOPER]
(对象的类型对象 {DEVELOPER}
).
你应该做的是:
if a_father.generating_type.is_conforming_to ({DEVELOPER}) then
因此将 TYPE [FATHER]
与 TYPE [DEVELOPER]
进行比较。
请注意,我假设它可以通过将 is_conforming_to
替换为 conforms_to
来工作,但是 class TYPE
引入了这个例程 is_conforming_to
更多具体参数类型。
我最好使用 built-in 机制来检查对象类型一致性:
if attached {DEVELOPER} a_father as dev then
dev.code
else
a_father.rest
end
并在前提条件中使用相同的方法:
attached {RELATED_DB_ENTITY} a_relationship_entity
对象测试做你想做的事:它检查附加到参数 a_relationship_entity
的对象类型是否符合类型 RELATED_DB_ENTITY
.