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=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6b85f5fd782a1527 X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: How to Use Abstract Data Types Date: 1998/05/05 Message-ID: <354F5C4D.DFF8286E@gsfc.nasa.gov>#1/1 X-Deja-AN: 350513971 Content-Transfer-Encoding: 7bit References: <6hm556$95u$1@nnrp1.dejanews.com> Content-Type: text/plain; charset=us-ascii Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 1998-05-05T00:00:00+00:00 List-Id: Matthew Heaney wrote: > > What's wrong with the iterator-as-type technique? > > package Libraries is > > type Root_Library is abstract tagged null record; > ... > type Library_Iterator (Library : access Root_Library'Class) is limited > private; > > function Is_Done (Iterator : Library_Iterator) return Boolean; > > function Get_Book (Iterator : Library_Iterator) return Book'Class; > > procedure Advance (Iterator : in out Library_Iterator); > ... > end; > > declare > Library : aliased My_Library; > Iterator : Library_Iterator (Library'Access); > begin > ... > while not Is_Done (Iterator) loop > ... Get_Book (Iterator) ... > Advance (Iterator); > end loop; > ... > end; > > This is the technique I use throughout the ACL. In particular, it's the > technique I used for the B-tree packages a few cla readers have asked for. I like this technique, too, but one problem I have with it is it assumes read-write access to the Library. Suppose I want to implement Find: function Find (Title : in String; Library : in My_Library) return Book'class is Iterator : Library_Iterator (Library'access); -- illegal begin ... end Find; Since the Library parameter is "in", you cannot use it as the access discriminant. The only way around this (that I have found) is to restructure Find to be a procedure, with Library "in out" and the Book result as an "out" parameter. With the generic package iterator: generic type Some_Library is new Library with private; This_Library: in Some_Library; package Iterator is function More return Boolean; function Next return Book'Class; end Iterator; "This_Library" is now read-only. Is there a good way to get read-only access with Iterator_Type? -- Stephe