eclipse-archived / ceylon-sdk

Standard platform modules belonging to the Ceylon SDK
http://www.ceylon-lang.org
Apache License 2.0
72 stars 60 forks source link

putIfAbsent etc for MutableMaps #692

Open dlkw opened 7 years ago

dlkw commented 7 years ago

Interface MapMutator<Key, Item> should have a method

shared formal Anything putIfAbsent(Key key, Item item);

which should be refined in MutableMap as

shared formal Item putIfAbsent(Key key, Item item);

There should be also MapMutator methods along the lines of

    shared formal Anything computeIfAbsent(Key key, Item(Object) comp);
    shared formal Anything computeIfPresent(Key key, Item(Object, Object) comp);

which should be refined in MutableMap somehow like (as close as possible to)

    shared formal Item computeIfAbsent(Key key, Item(Key) comp);
    shared formal Item computeIfPresent(Key key, Item(Key, Item) comp);

The latter is unfortunately not possible as written here due to covariance/contravariance clash.

This would not only ease the use of mutable maps in situations where a value must be put into the map when there is no value mapped to the key yet, but also enable the atomical usage of Java concurrent Maps in the same manner as standard Maps.