Iterators for D
Bill Baxter
dnewsgroup at billbaxter.com
Mon Nov 6 22:54:12 PST 2006
Walter Bright wrote:
> Kirk McDonald wrote:
>
>
>>> I think it does provide enough, in fact, it will be less wacky than
>>> in C++ (look at the wretched iterator/const_iterator dichotomy).
>>> It'll work with core arrays, and will not need those typedefs.
>> Without the ability to overload the dereference operator, any attempt
>> to write a class that behaves like a pointer in D will be unwieldy, or
>> at least ugly. Admittedly, this isn't much of an issue when just using
>> foreach, but might be an issue with any kind of STL-like algorithms
>> library. (Which is what you're inviting when using C++'s iterator
>> semantics.)
>
> I still think it isn't necessary, but you might be thinking of a use
> case I haven't. Can you provide an example?
The only thing that comes to mind is if you get multiple indirections.
For instance, in C++ I frequently end up creating things like vectors of
lists or lists of vectors. Or vectors of pointers. Or sets of list
iterators. Then to actually use the things I need two dereferences.
But with built-in GC, you don't really need smart pointers so often. And
with "dot-is-all-you-need" that also eliminates many cases for dereferences.
But still I'm sure you could come up with situations where to get at the
thing pointed to you'd need a string of [0]'s
iteriteriter[0][0][0]
Actually, in some ways it's an improvement over the prefix deref
operator because it reads consistently from left to right. In c++ if
you had iterator to vector of iterator and you wanted to deref it you'd
need:
*((*iterveciter)[i])
Or maybe the inner parens are unnecessary. I can't recall, which is
another reason why using postfix for everything is better -- no question
about precedence rules.
So I'm sold on dereferencing being a postfix operation. But I would
still like it better if there were some way to get a compile-time error
if I try to do
iter[2] // Error! This iterator isn't random access!
Would introducing a special symbol be of any use? Like [*]?
iter[*]
It would just compile to opIndex(0), but it declares the intent "this is
dereferencing, not arbitrary indexing". I guess it's kind of pointless
if there's no compiler support for enforcing its use. I guess it would
only be useful if there were an opDeref.
Anyway, whatever you do with dereferencing -- I'm convinced you should
absolutely not make it a unary prefix operator. It should be postfix.
--bb
More information about the Digitalmars-d
mailing list