www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17526] New: Add a set method for AA

https://issues.dlang.org/show_bug.cgi?id=17526

          Issue ID: 17526
           Summary: Add a set method for AA
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: qs.il.paperinik gmail.com

One cannot add values to an AA typed const(V)[K] because aa[key] is typed
const(V). Add a method that has the semantics of this

V set(K, V)(ref const(V)[const(K)] aa, auto ref const(K) key, lazy const(V)
value)
{
    if (auto valPtr = key in aa) return *valPtr;
    // cast away const as initialization is okay:
    return (cast() aa[key]) = value();
}

but without double lookup and without unsafe cast.

The question arose here:
https://forum.dlang.org/post/rwklxqwbsjzmcugsrxfq forum.dlang.org

--
Jun 19 2017