(Map|Set|WeakMap)#set() returns `this` ?

Jussi Kalliokoski jussi.kalliokoski at gmail.com
Thu Dec 6 00:48:45 PST 2012


On Wed, Dec 5, 2012 at 10:43 PM, Rick Waldron <waldron.rick at gmail.com>wrote:

>
>
>
> On Wed, Dec 5, 2012 at 3:26 PM, Domenic Denicola <
> domenic at domenicdenicola.com> wrote:
>
>> Readability or library preference aside, I still think it's bizarre that
>>
>> map.set(key, val)
>>
>> is analogous to
>>
>> (dict[key] = val, dict)
>>
>> and not to
>>
>> dict[key] = val
>>
>> When I'm using a fluent library like jQuery or a configuration DSL like
>> those in the npm packages surveyed, I can see the attraction of chaining.
>> But when I am using a basic primitive of the language, I expect uniformity
>> across primitives.
>>
>
> This argument won't hold when the language doesn't make any such
> "uniformity" promises, eg.
>
> array.push(val); // new length
> array[ array.length - 1 ] = val; // val
>

That's just a bad analogy, because that's not what push does, since it has
a variadic argument (admittedly, I don't think returning the length is
useful anyway, but if it returned the value, should it return the first,
last, or all of them?). And if it comes down to precedents in the language,
even Array#forEach() returns undefined, contrary to popular libraries out
there. Let's keep some consistency here.

I agree with you, fear-driven design is bad. But don't you agree that if
there's chaining, it's better done at language level rather than having all
APIs be polluted by `this` returns? After all, the APIs can't guarantee a
`this` return, since they might have something actually meaningful to
return, otherwise we might as well just replace `undefined` with `this` as
the default return value.

We could introduce mutable primitives so that meaningful return values
could be stored in arguments, kinda like in C, but instead of error values,
we'd be returning `this`, heheheh. :)

I'm curious, do you have any code examples of maps/sets that could be made
clearer by chaining?

Cheers,
Jussi



>
> Rick
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121206/78c1e7bb/attachment.html>


More information about the es-discuss mailing list