Feature to get or add value to an associative array.

Giles Bathgate giles.bathgate at gmail.com
Wed Apr 18 16:04:13 UTC 2018


On Wednesday, 18 April 2018 at 09:41:48 UTC, Nick Treleaven wrote:
> How do you implement this if the function returns with ref:

I understand where you are coming from, but I am not sure it is 
appropriate to shoehorn every use case into one api. I think 
actually what you are describing here is the AddOrUpdate style 
method 
https://msdn.microsoft.com/en-us/library/ee378665(v=vs.110).aspx

Perhaps the implementation could be:

     void createOrUpdate(K, V)(ref V[K] aa, K key, V delegate() 
create, void delegate(V*) update);


     C newc;
     aa.createOrUpdate("key", {
         /* set *p */
         newc = new C;
         return newc;
     },
     (C* u){
         // read *p
         newc = *u;
         // set *p
         *u = new C;
     });
     assert(aa["key"] == newc);

¯\_(ツ)_/¯


More information about the Digitalmars-d mailing list