简体   繁体   中英

Eiffel: a way to check type conformance with a given CLASS_NAME

I'm trying to do something as

work (a_father: FATHER)
    do
        if a_father.conforms_to ({DEVELOPER}) then
           a_father.code
        else
           a_father.change_job ({DEVELOPER})
        end
    end

在此处输入图片说明 在此处输入图片说明

the compilation works, but in my implementation @runtime it doesn't pass. What did I mistype?

The problem in your example is that you are trying to see whether the type FATHER (type of the object a_father ) conforms to the type TYPE [DEVELOPER] (the type of the object {DEVELOPER} ).

What you should do is:

if a_father.generating_type.is_conforming_to ({DEVELOPER}) then

hence comparing TYPE [FATHER] with TYPE [DEVELOPER] .

Note that I would assume that it would work by replacing is_conforming_to by conforms_to , but class TYPE introduced this routine is_conforming_to with a more specific argument type.

I'd better use a built-in mechanism to check object type conformance:

if attached {DEVELOPER} a_father as dev then
     dev.code
else
     a_father.rest
end

And use the same approach in the precondition:

attached {RELATED_DB_ENTITY} a_relationship_entity

The object test does what you would like to: it checks that the type of the object attached to argument a_relationship_entity conforms to type RELATED_DB_ENTITY .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM