Search code examples
arrayseiffel

Eiffel: How to wipe_out an ARRAY object without recreating it


Trying to do something like

a: ARRAY[STRING]

create a.make_empty
a.put("foo foo fool")
a.wipe_out

Do I have to? or is there another way as STRING doesn't seem to have a .has_default

create a.make_empty
a.put("foo foo fool")
create a.make_empty

compilation error


Solution

  • The most straightforward way is to use keep_head (n). It keeps only first n items, therefore, when n = 0, all items are removed altogether:

        a.keep_head (0)
    

    Another way is to use a creation procedure, for example, make_empty as a regular one. It is going to set an array to the state of a newly created one:

        a.make_empty
    

    However, this approach looks a bit odd. And it can change lower index of the array. So, keep_head is preferable.

    Note. ARRAYED_LIST is a good alternative to ARRAY: it has almost all features of ARRAY, is more flexible, has other features, and wipe_out among them.