简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM