Indexing access for Lists and Maps considered harmful?

spamolovko spamolovko at gmail.com
Tue Jun 23 06:59:26 PDT 2009


Better way for
m[a] = n[a] = x;

is to become this byte-code
n.put(a, x);
m.put(a, n.get(a));

this is because
m[a] = n[a];

must be translate into:
m.put(a, n.get(a));


So i do not see any problems with side-effect grammar like

String x = coll[0] = "ABC";

because it become:
coll.put(0, "ABC");
String x = coll.get(0);


PS i remember that coin project had discussion about usage of special 
interface for "indexing access". That interface have to use it's own 
methods for access to data, so no problems with get semantic.

Example:

public interface Indexer<KeyType, ValueType> {

    public ValueType indexGet(KeyType key);

    public void indexSet(KeyType key, ValueType value);

}

import java.util.HashMap;

public class MyCoolMap<KeyType, ValueType> extends HashMap<KeyType, 
ValueType>
        implements Indexer<KeyType, ValueType> {

    public ValueType indexGet(KeyType key) {
        return get(key);
    }

    public void indexSet(KeyType key, ValueType value) {
        put(key, value);
    }

}


public class Usage {

    static public void main(String[] args) {
        MyCoolMap<String, String> map = new MyCoolMap<String, String>();

//        map["A"] = map["B"] = "hello";
        map.indexSet("B", "hello");
        map.indexSet("A", map.indexGet("B"));
    }

}

BR.



More information about the coin-dev mailing list