inheritanceeiffel

How to inherit from HASH_TABLE in Eiffel?


I want to make a derived class of HASH_TABLE which implements a few additional features. I tried implementing it like this:

class HASH_TABLE2[G->HASHABLE]
inherit
    HASH_TABLE[G,G]
        rename
            make as make_2
        end
create
    make
feature
    make (capacity_ : INTEGER)
        do
            make_2(capacity_)
        end
end

I get this error:

Error: Creation instruction uses call to improper feature.

Feature name: make_2 (n: INTEGER_32)
Line: 1156
      do
->      create Result.make (n)
        if object_comparison then

I don't understand why. If I do the same with inherit ARRAY[G], then it works fine.


Solution

  • The issue is that the feature empty_duplicate (mentioned in the error message) creates a new instance of a hash table using the original creation procedure make. However, this feature is no longer a creation procedure in HASH_TABLE2. The solution is to redefine empty_duplicate accordingly:

    class HASH_TABLE2 [G -> HASHABLE]
    
    inherit
        HASH_TABLE [G, G]
            rename
                make as make_2
            redefine
                empty_duplicate
            end
    
    create
        make
    
    feature
        
        make (capacity_: INTEGER)
            do
                make_2 (capacity_)
            end
    
    feature {NONE} -- Duplication
    
        empty_duplicate (n: INTEGER_32): like Current
                -- <Precursor>
            do
                create Result.make (n)
                if object_comparison then
                    Result.compare_objects
                end
            end
    
    end
    

    Note that although the body of empty_duplicate redeclaration looks identical to the original one, it calls make from HASH_TABLE2 that has nothing to do with make from HASH_TABLE because the latter was renamed into make_2.

    This brings us to the question whether make from HASH_TABLE could remain the creation procedure in HASH_TABLE2. Unless the given example is a simplification, this is feasible and compiles without an issue:

    class HASH_TABLE2 [G -> HASHABLE]
    inherit
        HASH_TABLE [G, G]
    create
        make
    end