View mode: basic / threaded / horizontal-split · Log in · Help
March 30, 2011
multiple-item traversal ranges
Hello,

I take the opportunity of opSlice() now seemingly working (see // thread) to 
talk about one of the last annoying points for everyday use of ranges for 
traversal: the absence of "key-ed traversal", or more generally "multi-item 
traversal", as in:
    foreach (k,v ; myTable) {...}
After some thought on the topic, I would push for a solution where collection 
types which should provide such multi-item traversal methods, would define a 
specific struct, eg:

struct Map (K,V) {
    struct Pair { K key ; V value; }
    struct Range {
        @property Pair front () {
            assert (! this.empty);
            Return Pair(...);
        }
        ...
    }
    Range opSlice () {
        return Range(this);
    }
    ...
}

The advantages of this solution are:
1. It only uses struct: a builtin, simple & efficient feature.
2. It works *now*, as is; it is just a standard to promote.
3. It trivially allows multiple ranges for the same collection type.
4. It is easy to use & understand on the caller side:

    foreach (pair ; map) {
	doSomethingWith(pair.key, pair.value);
    }

    foreach (record ; table) {
	doSomethingWith(record.name, record.phone, record.mail);
    }

Denis
-- 
_________________
vita es estrany
spir.wikidot.com
Top | Discussion index | About this forum | D home