Bug 12139 – AA.update

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-12T04:26:13Z
Last change time
2018-10-16T18:51:24Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-02-12T04:26:13Z
This is a possible enhancement. I don't know how much useful it is, but it seems worth thinking about. One recent change in the Erlang language is the syntax to handle associative arrays: > Updating a map is done as follows: > > NewX = X#{ key1 => Val1, ... KeyN := ValN, ...} X is the old associative array. "=>" adds a new key, while ":=" updates an already existing key, and if it's not present an error is generated. So a spelling mistake cannot accidentally introduce a new key if you just want to update some value. This avoids a common bug. Similarly a simple "AA.update(key, val)" function in the D object module could be useful. It's a way to modify associative array values that requires the keys to be already present. This function modifies the value of a key-value if the key is present, and raises an exception if the key is missing.
Comment #1 by n8sh.secondary — 2018-10-16T18:51:24Z
PR #2162 added `require` and `update` (https://github.com/dlang/druntime/pull/2162). Also this issue pertains to druntime, not phobos.