Feature to get or add value to an associative array.

Jordan Wilson wilsonjord at gmail.com
Wed Apr 18 21:04:53 UTC 2018


On Wednesday, 18 April 2018 at 17:19:45 UTC, Giles Bathgate wrote:
> On Sunday, 15 April 2018 at 22:55:41 UTC, Giles Bathgate wrote:
>> Time for a bikeshed discussion...
>
> I have had some thoughts about the name and would like to share 
> my idea.
>
> Firstly to summarise here are the names that have been 
> found/discussed so far:
>
> C#                   : GetOrAdd
> Java                 : computeIfAbsent
> Python               : setdefault
> Rust                 : entry(key).or_insert_with
>
> Jordan Wilson        : getOrAdd, getOrSet
> User1234             : valueOrDefault
> Nicholas Wilson      : getOrInsert
> Steven Schveighoffer : getPtr, getRef, getInitialized
> Nick Treleaven       : slot
> MrSmith              : getOrCreate
>
> My latest idea is just a working title, but I think I prefer it 
> to my original getOrAdd.
>
> Giles Bathgate       : require
>
> My thinking is that if you `require` there to be a value in the 
> associative array, then you should have the ability to add one 
> iff it doesn't exist. It name also has parallels with the term 
> in other programming languages to require a module, meaning to 
> import it if it hasn't already been loaded.

Thinking seems sound, although having a name starting with "get" 
does have the advantage of being more related to the existing get.

Weirdly, in all cases if I replace "Or" with "Else", it seems to 
read easier for me... getElseAdd, getElseCreate, etc.

Ultimately I'm not too bothered with any name really, I'm just 
looking forward to using it eventually :-)

Jordan


More information about the Digitalmars-d mailing list