ceylon-sdk
ceylon-sdk copied to clipboard
putIfAbsent etc for MutableMaps
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.