associative arrays: iteration is finally here

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Wed Oct 28 09:39:47 PDT 2009


yigal chripun wrote:
> Andrei Alexandrescu Wrote:
> 
>> Walter has magically converted his work on T[new] into work on making 
>> associative arrays true templates defined in druntime and not considered 
>> very special by the compiler.
>>
>> This is very exciting because it opens up or simplifies a number of 
>> possibilities. One is that of implementing true iteration. I actually 
>> managed to implement last night something that allows you to do:
>>
>> int[int] aa = [ 1:1 ];
>> auto iter = aa.each;
>> writeln(iter.front.key);
>> writeln(iter.front.value);
>>
>> Two other iterations are possible: by key and by value (in those cases 
>> iter.front just returns a key or a value).
>>
>> One question is, what names should these bear? I am thinking of makign 
>> opSlice() a universal method of getting the "all" iterator, a default 
>> that every container must implement.
>>
>> For AAs, there would be a "iterate keys" and "iterate values" properties 
>> or functions. How should they be called?
>>
>>
>> Thanks,
>>
>> Andrei
> 
> that looks neat. What's the mechanism to tie the templates to the syntax? 
> 
> I don't understand why all containers must provide a default range. What's the default for a binary tree?  it has several orderings (pre, post, infix) but i can't say that one is "more default" the the other.  

The cheapest to implement. As I mentioned in the Bogo containers 
discussion, I think any container must implement some way of iterating 
it. A container without an iterator is not a container.

Andrei



More information about the Digitalmars-d mailing list