From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!ukc!tcdcs!swift.cs.tcd.ie!ccvax.ucd.ie!louboutn From: louboutn@ccvax.ucd.ie (Sylvain Louboutin, University College Dublin, Ireland) Newsgroups: comp.lang.ada Subject: Re: On quitting an iterator prematurely Message-ID: <974.26222541@ccvax.ucd.ie> Date: 10 Apr 90 18:26:09 GMT References: <9004041704.AA17624@ajpo.sei.cmu.edu> <1151@enea.se> Organization: University College Dublin List-Id: In article <1151@enea.se>, sommar@enea.se (Erland Sommarskog) writes: > Norman H. Cohen (NCOHEN@IBM.COM) writes: >>Andre' Alguero asks about how to write a procedure to iterate over >>a set of discrete elements, specifying the actions to be performed >>at each iteration at the point at which the procedure is invoked. [...] > Some years ago I wrote a binary tree package with a similar concept. > When I was to use the package I discovered one thing I had missed. > There was no way to interrupt the iteration. One way is of course > to rewrite the package so that Process_one_element should take a > second parameter > Continue : OUT boolean; > The problem with this is that if the user in 90% of the time wants to > iterate through the entire set, this second parameter just clutters > up his code, and he may even forget to initiate it with interesting > results, unless the compiler catches the error. [Erland then proposes to use an exception to interrupt the iteration] Why don't you write the procedure like this: (note that the 'continue' paramter mode is IN OUT instead o OUT) generic with procedure Process_One_Element (Element: in Element_Type; Continue : IN OUT boolean); procedure Process_Each_Element (Set: in Set_Type); procedure Process_Each_Element (Set: in Set_Type) is Continue: boolean := TRUE; -- other declaration... begin -- some statements loop -- some statements Process_One_Element (Current_Element, Continue); exit when not Continue; -- some statements end loop; -- some statements... end Process_Each_Element; The user (i.e. the programmer of the procedure 'Process_One_Element') doesn't need to bother about the parameter continue (except that s/he has to include it in the header of the procedure, but there is no need to set a value to it in the procedure Process_Each_Element) if s/he wants to iterate through all the set... -- -- Sylvain R.Y. Louboutin fax: (+353-1) 69-72-62 -- Dept of Computer Science, University College, Dublin 4, -- Ireland -- -- LOUBOUTN@CCVAX.UCD.IE, S_LOUBOUTIN@CS.UCD.IE, SLOUBH91@IRLEARN.BITNET