Pipo
Pipo

Reputation: 5093

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

enter image description here enter image description here

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

Upvotes: 0

Views: 156

Answers (2)

Alexander Kogtenkov
Alexander Kogtenkov

Reputation: 5810

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.

Upvotes: 1

Eric Bezault
Eric Bezault

Reputation: 141

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.

Upvotes: 1

Related Questions