From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-0.9 required=3.0 tests=BAYES_00, PP_MIME_FAKE_ASCII_TEXT autolearn=no autolearn_force=no version=3.4.5-pre1 Path: eternal-september.org!reader02.eternal-september.org!feeder.eternal-september.org!gandalf.srv.welterde.de!news.jacob-sparre.dk!franka.jacob-sparre.dk!pnx.dk!.POSTED.rrsoftware.com!not-for-mail From: "Randy Brukardt" Newsgroups: comp.lang.ada Subject: Re: Generalized Loop Iteration and User-Defined Indexing with more than two parameters. Date: Fri, 6 Nov 2020 18:58:51 -0600 Organization: JSA Research & Innovation Message-ID: References: <86361qwvht.fsf@stephe-leake.org> Injection-Date: Sat, 7 Nov 2020 00:58:53 -0000 (UTC) Injection-Info: franka.jacob-sparre.dk; posting-host="rrsoftware.com:24.196.82.226"; logging-data="10115"; mail-complaints-to="news@jacob-sparre.dk" X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.5931 X-RFC2646: Format=Flowed; Response X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.7246 Xref: reader02.eternal-september.org comp.lang.ada:60560 List-Id: "Blady" wrote in message news:ro3qms$1vdh$1@gioia.aioe.org... > Le 03/11/2020 à 12:05, Stephen Leake a écrit : ... > Ada RM 5.5.1 "User-Defined Iterator Types" says: > "The Constant_Indexing aspect (if any) of an iterable container type T > shall denote exactly one function with the following properties: > ... > if there are more than two parameters, the additional parameters all have > default expressions." > So I defines: > function Get (Self : UXString; Position : Cursor; Substitute : in > Character := '&') return Character; > and then: > for I in S1.Iterate loop > C := S1 (I, '@'); > F := S1 (I) = 'h'; > Put_Line (Character'pos (C)'img & F'img); > end loop; > for CC of S2 loop > C := CC; > Put_Line (Character'pos (C)'img); > end loop; > > However, I have 3 questions about iteration: > 1) When using Default_Iterator aspect, both Iterator_Element and > Constant_Indexing have to be defined also. > If I understand why Constant_Indexing at least has to be defined, what is > the purpose of Iterator_Element? Iterator functions work on cursors. The "of" form works on elements. In order to figure out what constant indexing function to use in the element iterator, we need to know both the parameter and result types. (Remember that there can be many overloaded indexing functions, you're not restricted to a single function [just a single name].) > 2) In Ada.Iterator_Interfaces, why Has_Element is a generic formal > parameter instead of an abstract function of Forward_Iterator interface? > As: > function Has_Element > (Object : Forward_Iterator; > Position : Cursor) return Boolean is abstract; > The current way requires that Cursor type has the container reference > embedded and cannot be a simple index type. I suppose other designs could have been chosen. The generic one was the original proposal; probably the person that designed it doesn't use interfaces very much. (I think that was me. ;-). If I recall correctly, the original interface design didn't work at all, and the generic was a fix. But I don't remember any of the details anymore (could look them up, but would need more motivation for that :-). > 3) Couldn't be the additional parameters of the Constant_Indexing function > without parameters defaults and a for ... of form as one of the > followings? > function Get (Self : UXString; Position : Cursor; Substitute : in > Character) return Character; > ... > for CC of S2 ('@') loop > C := CC; > Put_Line (Character'pos (C)'img); > end loop; > or less preferable: > for CC of S2 loop > C := CC ('@'); > Put_Line (Character'pos (C)'img); > end loop; I suppose, but that sort of thing would substantially complicate the mechanism, especially resolution. Personally, I think of the "of" form of iterators as a convinient shorthand to be used only in limited circumstances. The cursor form ("in"), the more traditional form, should be used when you have more complex needs. For instance, for the above: for CC in S2.Iterator loop C := S2(CC, '@'); Put_Line (Character'pos (C)'img); end loop; Trying to jam everything into the shorthand just complicates the implementation and makes it a lot harder for the reader. Randy. > Thanks for your answers, Pascal.