Search code examples
generic-programmingeiffel

Sharing a type with a generic class?


How do you declare a variable to be the same type as a type parameter used to instantiate a generic class? The following code does not compile:

class
    TEST [G, H -> INTEGER]

feature

    f (i: INDEXABLE [G, H])
        local
            y: H
        do
            y := i.lower -- Type error here.
        end

end

The compiler says that the source of the assignment is not compatible with target.


Solution

  • In the current implementation, INDEXABLE [G, H] inherits from TABLE [G, INTEGER]. As a result, lower is of type INTEGER, not H. And INTEGER does not conform to the formal generic type H of the class TEST. This explains the error.

    To me, it looks like a mistake in the declaration of class INDEXABLE. It should inherit from TABLE [G, H] instead. Then, the example code would compile.