comp.lang.ada
 help / color / mirror / Atom feed
* Why does Ada.Iterator_Interfaces specify Next as a function rather than a procedure?
@ 2017-09-06  7:49 Stephen Leake
  2017-09-06 21:50 ` Randy Brukardt
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Stephen Leake @ 2017-09-06  7:49 UTC (permalink / raw)


I have a fairly complex collection of stuff that I want to define an iterator for, so all clients access the stuff in the same order. The stuff is token definitions for a lexer; keywords, punctuation, whitespace, identifiers, numbers.

Initially, I defined my own Cursor type, that stores iterators to the various pieces, and manages the transitions between them. 

Thus the Cursor type has state, and the "Next" subprogram is a procedure. It 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_Name 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 since I would be lying to the compiler, I'm not clear what subtle bugs I might encounter. For example, the compiler might assume that it can do:

  Next_Cursor := Next (Cursor);

and Next_Cursor and Cursor have different values (ie refer to different items). I can't think of why it would want to do that for a loop, but since the 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 call 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 next 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 :)

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2017-09-07 23:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-06  7:49 Why does Ada.Iterator_Interfaces specify Next as a function rather than a procedure? Stephen Leake
2017-09-06 21:50 ` Randy Brukardt
2017-09-07  1:04   ` Stephen Leake
2017-09-07  1:29     ` Stephen Leake
2017-09-07 23:14       ` Randy Brukardt
2017-09-07  2:53 ` Charles H. Sampson
2017-09-07  6:16 ` briot.emmanuel
2017-09-07 23:17   ` Randy Brukardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox