Search code examples
generic-programmingeiffel

Eiffel: Invalid constraint for formal generic paramete


Following with strange patterns for some, can't I do that? The compiler says Invalid constraint for formal generic parameter

class PARENT[G -> CHILD[like Current]]

feature -- Access

    children: LIST[G]

end

class CHILD[H -> PARENT[like Current]]

feature -- Access

    father: H
end

to be able to do something like

class WIDOW_PARENT

inherit
    PARENT[BLACK_CHILD]

end

class BLACK_CHILD

inherit 
    CHILD[WIDOW_PARENT]

end

If I don't do it with genericity, I'd have to redefine the children collection from

  • children: LIST[CHILD] to children: LIST[BLACK_CHILD] into the WIDOW_PARENT class
  • father: PARENT to father: WIDOW_PARENT into the BLACK_CHILD class

instead of only specify it in the inherit clause... Hope it makes sense

Update

As I solved it with Alexanders answer, I'm stuck further doing a conformity check. I'm trying to set an HTTP router depending on entities and if its a child entity it should be able to do a http://host:port/entity/child_entity/id to get all child entities from entity. For that I'd like to add to the generic router a check. On something like ANY_PARENT_DB_ENTITY such as

if ({G}).conforms_to ({CHILD_DB_ENTITY[ANY_PARENT_DB_ENTITY]}) then
    friend.act_like_a_father 
else
    friend.act_like_a_mate
end

Solution

  • In contemporary Eiffel, anchored types cannot be used in formal generic constraints, thus the error. It's still possible to have mutual constraints by repeating class types explicitly:

    class PARENT [G -> CHILD [PARENT [G]]]
    class CHILD  [H -> PARENT [CHILD [H]]]
    

    With this change, the example compiles.