Search code examples
iterableeiffel

Returning ITERABLE type in Eiffel


I am trying to return Result type being ITERABLE[K]. All I know is that Iterable inherits from ITERATION_CURSOR, so that I made following unworking code but it doesn't compile.

obtainKey (v: V): ITERABLE[G]
    local
        myCollection: ITERABLE [G]
        myCursor:ITERATION_CURSOR[G]
    do
        create {ITERABLE[G]} myCursor
        Result := myCursor

My guess is that I have to do something like following, if it was c++ or Java,

ITERATION_CURSOR myCursor = new ITERABLE;

I don't know. My assumption could be wrong.

How can I do this kind of thing in Eiffel and make above code work?


Solution

  • The ITERABLE class is a deferred class (abstract in java) and a deferred class cannot be created. You have to use a class that is not deferred and that inherit from the ITERABLE class. Note that the ITERATION_CURSOR class is also deferred. What to use can change depending of what you need in your implementation. Here is an example using a LINKED_LIST:

    obtain_key (v:V): ITERABLE[G]
        local
            my_list:LINKED_LIST[G]
        do
            create my_list.make
            Result := my_list
        end