Feature to get or add value to an associative array.
    Giles Bathgate 
    giles.bathgate at gmail.com
       
    Tue Apr 17 22:33:18 UTC 2018
    
    
  
On Tuesday, 17 April 2018 at 21:40:55 UTC, Giles Bathgate wrote:
> Rust calls its version of this function `or_insert_with` (blegh)
Of course, a rustic API could be built atop this PR:
template entry(K, V)
{
     static struct Entry
     {
         alias get this;
         V[K] aa;
         K key;
         V get()
         {
             return aa[key];
         }
         V orInsert()
         {
             return aa.getOrAdd(key);
         }
         V orInsertWith(lazy V value = V.init)
         {
             return aa.getOrAdd(key, value);
         }
     }
     Entry entry(ref V[K] aa, K key)
     {
         return Entry(aa,key);
     }
}
void main()
{
     class C{}
     C[string] aa;
     auto v1 = aa.entry("foo");
     auto v2 = aa.entry("bar").orInsert();
     auto v3 = aa.entry("baz").orInsertWith(new C);
}
But I think, this would be out of scope.
    
    
More information about the Digitalmars-d
mailing list