C++ / Why Iterators Got It All Wrong
Mark via Digitalmars-d
digitalmars-d at puremagic.com
Fri Sep 1 13:34:32 PDT 2017
On Tuesday, 29 August 2017 at 12:50:08 UTC, Robert M. Münch wrote:
> Maybe of interest:
> https://www.think-cell.com/en/career/talks/iterators/#1
>
> I haven't read everything, so not sure if it worth to take a
> look.
Iterators have many problems. Andrei's talk some years ago,
titled "Iterators Must Go", points out many of them in a clear
fashion. I think most of the problems stem from the fact that
they are inherently a leaky abstraction. Iterators basically
treat all data structures as a singly/doubly linked list or as
arrays (if they allow random access). There is no natural or
intuitive way of describing, say, a tree or an arbitrary graph as
a list/array. Ranges and cursors try to solve some of the issues
but they still have this fundamental problem.
When I think of a good iteration interface, the only thing that
comes to mind is SQL queries (= relational algebra, more or
less). You have a few basic operators (Select, From, etc.) for
querying that are intuitive, simple and safe. Furthermore, they
compose elegantly, allowing you to express fairly complicated
queries using these few basic operators. It's a true abstraction
- I don't know and I don't care if the operators are implemented
underneath using iterators, ranges, cursors or whatever (of
course, sometimes I do care, when performance is a priority...).
It may be unrealisic to expect such a nice abstraction of
iteration for other data structures, not to mention a universal
one which will work for all interesting data structures. Still,
if and when I can dispense with iteration altogether, I'm happy
to do so.
More information about the Digitalmars-d
mailing list