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.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.129.173.75 with SMTP id l11mr1064169ywk.52.1504684189976; Wed, 06 Sep 2017 00:49:49 -0700 (PDT) X-Received: by 10.36.172.79 with SMTP id m15mr237045iti.3.1504684189923; Wed, 06 Sep 2017 00:49:49 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!weretis.net!feeder4.news.weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!h15no86450qta.1!news-out.google.com!c139ni118itb.0!nntp.google.com!127no184885itw.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Wed, 6 Sep 2017 00:49:49 -0700 (PDT) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=76.218.37.33; posting-account=W2gdXQoAAADxIuhBWhPFjUps3wUp4RhQ NNTP-Posting-Host: 76.218.37.33 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <693d41af-1da7-4c47-825a-198a082aaf9a@googlegroups.com> Subject: Why does Ada.Iterator_Interfaces specify Next as a function rather than a procedure? From: Stephen Leake Injection-Date: Wed, 06 Sep 2017 07:49:49 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:47946 Date: 2017-09-06T00:49:49-07:00 List-Id: I have a fairly complex collection of stuff that I want to define an iterat= or for, so all clients access the stuff in the same order. The stuff is tok= en definitions for a lexer; keywords, punctuation, whitespace, identifiers,= numbers. Initially, I defined my own Cursor type, that stores iterators to the vario= us pieces, and manages the transitions between them.=20 Thus the Cursor type has state, and the "Next" subprogram is a procedure. I= t is _not_ just "return node.next", but more like: if keywords not done, do Next (Keywords) elsif punctuation not done, do Next (Punctuation) with more logic that does First (Punctuation) at the right time. And so on = for the rest of the stuff. Then I learned about Ada 2012 Iterators (allowing things like "for Token_Na= me of Tokens"), so naturally I tried to define one for this collection. It all goes well until I hit the definition of Ada.Iterator_Interfaces.Next= ; it must be a function, with an "in" parameter. I can work around this with an access type, but that's just annoying. And s= ince I would be lying to the compiler, I'm not clear what subtle bugs I mig= ht encounter. For example, the compiler might assume that it can do: Next_Cursor :=3D Next (Cursor); and Next_Cursor and Cursor have different values (ie refer to different ite= ms). I can't think of why it would want to do that for a loop, but since th= e current definition of Next allows it, I worry about it. If I don't use an access type, I'll have to copy the Cursor state in each c= all to Next, then modify it and return that. This is not in a time critical= loop, so that's not really a problem, but it seems a silly waste. And my n= ext use case could very well be in a time critical loop. The Annotated Ada Reference Manual doesn't shed any light on this. Would it be a problem to change Ada.Iterator_Interfaces.Next (and Previous)= to take an "in out" parameter for the Cursor? Or provide an alternate procedure Next? I guess that would have to be in a = different package. -- Stephe (I realize I'm over five years late with this, but better late than never := )