A different semantics for WeakMap#get default value
Allen Wirfs-Brock
allen at wirfs-brock.com
Tue Jan 17 10:07:23 PST 2012
On Jan 17, 2012, at 3:45 AM, Herby Vojčík wrote:
> Brendan Eich wrote:
>> Clearly(!) a set-if-not-present method should not be misnamed "get".
>>
>> I like the optional sentinel-meaning-not-found for get, and setDefault
>> per Python as Tab pointed out. Agree they should not be merged into one
>> API. Bikeshedding setDefault at leisure (in background in my head ;-).
>
> Like:
> getIfAbsentSet(key, dflt)
> with dflt being mandatory?
> Still there is question of laziness of dflt, the original #get:ifAbsentPut: has the block as its second argument.
>
The Smalltalk solution to this problem is the
at: key ifAbsent: block
method.
This subsumes both get with default value and get create if missing use cases plus others. In JS we might say:
col.getIfAbsent(key) {|| 42}; //use 42 if no present
col.getIfAbsent(key) {|| col.defaultValue}; //have collection provide the default value
col.getIfAbsent(key) {|| col.put(key, 42); 42} //if key isn't there add it with an appropriate vaue
Allen
>>
>> /be
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
More information about the es-discuss
mailing list