I prefer variant 2. Regarding the compat problem, I think using `+=() as fallback if step() doesn't exist is a more viable solution.
/ Henrik Grubbström (Lysator)
Previous text:
2003-09-04 14:49: Subject: Iterator interface
Speaking about iterators and quirks, there's one in the iterator interface too: The step operation uses `+=, which is fairly logical for the iterator implementor but not for users; the only way to explicitly step an iterator is like this:
iter->`+= (1);
Clumsy syntax, and it invites to make the mistake of using the operator += directly:
iter += 1;
That won't work since it normally copies the iterator instead of changing it destructively. (Remember that a += b is equivalent to a = a + b except that a is evaluated only once. Hence there's no relation between the operator += and the lfun `+=.)
We ought to fix this somehow. I can see two alternatives:
Add a global step_iterator function to accompany get_iterator. It'd be used like this
step_iterator (iter, 1);
and do an lfun call to `+= (which would work even if it's static).
Actually, the function could be used to make an lfun call to `+= in any object, so perhaps it should be called "destructive_add" or something like that, but that'd be just as unintuitive in the iterator context as iter->`+=(1).
Rename `+= to "step" in the iterator interface, so that one can do
iter->step (1);
instead.
The necessary compatibility goo to cope with iterators that only define `+= is complicated, though: A #pike 7.4 directive in the file where the iterator is defined should be enough. That means that the compiler has to automatically add an alias "step" for `+= in any class that seems to be an iterator. Messy.
/ Martin Stjernholm, Roxen IS