How to resolve the algorithm Constrained genericity step by step in the Eiffel programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Constrained genericity step by step in the Eiffel programming language

Table of Contents

Problem Statement

Constrained genericity or bounded quantification means that a parametrized type or function (see parametric polymorphism) can only be instantiated on types fulfilling some conditions, even if those conditions are not used in that function. Say a type is called "eatable" if you can call the function eat on it. Write a generic type FoodBox which contains a collection of objects of a type given as parameter, but can only be instantiated on eatable types. The FoodBox shall not use the function eat in any way (i.e. without the explicit restriction, it could be instantiated on any type). The specification of a type being eatable should be as generic as possible in your language (i.e. the restrictions on the implementation of eatable types should be as minimal as possible). Also explain the restrictions, if any, on the implementation of eatable types, and show at least one example of an eatable type.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Constrained genericity step by step in the Eiffel programming language

Source code in the eiffel programming language

deferred class
    EATABLE

feature -- Basic operations

    eat
            -- Eat this eatable substance
        deferred
        end
end


class
    APPLE

inherit
    EATABLE

feature -- Basic operations

    eat
            -- Consume
        do
            print ("One apple eaten%N")
        end
end


class
    PEAR

inherit
    EATABLE

feature -- Basic operations

    eat
            -- Consume
        do
            print ("One pear eaten%N")
        end
end


class
    FOOD_BOX [G -> EATABLE]

inherit
    ARRAYED_LIST [G]

create
    make

end


    my_apple_box: FOOD_BOX [APPLE]


    my_refrigerator: FOOD_BOX [EATABLE]


class
    APPLICATION

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        do
            create my_apple_box.make (10)
            create one_apple
            create one_pear
            my_apple_box.extend (one_apple)
--          my_apple_box.extend (one_pear)
            across
                my_apple_box as ic
            loop
                ic.item.eat
            end
        end

feature -- Access

    my_apple_box: FOOD_BOX [APPLE]
            -- My apple box

    one_apple: APPLE
            -- An apple

    one_pear: PEAR
            -- A pear
end


--              my_apple_box.extend (one_pear)


  

You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Ela programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Read a specific line from a file step by step in the C++ programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Common Lisp programming language