Feature to get or add value to an associative array.

Nick Treleaven nick at geany.org
Fri Apr 20 08:37:59 UTC 2018


On Wednesday, 18 April 2018 at 16:04:13 UTC, Giles Bathgate wrote:
> I understand where you are coming from, but I am not sure it is 
> appropriate to shoehorn every use case into one api.

It is not shoehorning, the difference here is just returning by 
ref vs pointer. I understand that, as the pointer address is not 
needed by the caller, it is cleaner to use ref. The problem is 
that local refs are not supported by D, and so I would need to do 
this (at least in safe code):

import std.functional : unaryFun;
bool ins;
aa.update("key", {ins = true; return 1;}).unaryFun!((v){ if (ins) 
v++; });

This seems pretty convoluted. It could be made a bit better if D 
supported UFCS for lambdas, then unaryFun wouldn't be needed.

BTW, I like the name `update`.

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

Again, would the delegate calls always be inlined, in all cases?

I think having a low-level API in druntime is appropriate, it's a 
runtime library after all.



More information about the Digitalmars-d mailing list