Search code examples
polymorphismgeneric-programmingeiffel

Eiffel: non-compatible actual argument in feature call


I don't understand why it is so.

  • If I say attached {G} it works
  • If I say nothing which would be the expected behavior for me calling_entity: detachable RELATED_DB_ENTITY should be conform (once attached) to G which is -> DB_ENTITY
  • If I say DB_ENTITY it doesn't
  • If I say RELATED_DB_ENTITY either does it pass

Why do I have to specify {G}???

SIT_HANDLER

class
    SIT_HANDLER[G -> DB_ENTITY create default_create, make_from_db_service, make_from_json end]

feature --

    some_feature
        do
            if attached {G} l_rest_request.calling_entity as l_calling_entity then
                db_service.set_item_prototype (l_calling_entity) -- Complains here!!!!!!!!!!!!
                db_service.load_with_relationships (l_rest_request)
            ...
        end
end -- class

REST_REQUEST

class
    REST_REQUEST

feature -- Access
    calling_entity: detachable RELATED_DB_ENTITY -- RELATED_DB_ENTITY inherits DB_ENTITY
...
end -- class

DB_SERVICE

class
    DB_SERVICE [G -> DB_ENTITY create default_create, make_from_db_service, make_from_json end]

feature -- Status setting

    item_prototype: G

    set_item_prototype (v: like item_prototype)
        do
            item_prototype := v
        ensure
            item_prototype = v
        end

...
end -- class

enter image description here


Solution

  • The type RELATED_DB_ENTITY does not conform to the type G.

    Here is an example why. Suppose there is a class FOO that inherits from DB_ENTITY and has all required creation procedures. FOO and RELATED_DB_ENTITY do not conform to each other. For the type SIT_HANDLER [FOO], the argument of the feature db_service.set_item_prototype has type FOO whereas the type of the expression l_rest_request.calling_entity is RELATED_DB_ENTITY. It is not allowed to assign an expression of type RELATED_DB_ENTITY to the entity of type FOO.