Set.prototype.toggle

Jordan Harband ljharb at gmail.com
Sun Oct 29 16:19:55 UTC 2017


`Set.prototype.delete` returns a boolean indicating whether a deletion was
done.

I think the minor convenience of "toggle" (avoiding an `if` statement, or a
ternary, in a single use case) isn't worth the complexity and confusion of
having an API with a boolean argument - which is something that there's
zero precedent in the language for, even if the ecosystem has it, and
something that I suspect would block any progress on such an API.

On Sun, Oct 29, 2017 at 6:08 AM, Cyril Auburtin <cyril.auburtin at gmail.com>
wrote:

> My use-case was a choice-list form, with React. I used a Set to store the
> current selected values in state.
>
> It's updated with checkbox 'change' event, and depending on `.checked`,
> you delete or add (so .toggle).
>
> When you have several of those choice-lists, it becomes even more helpful.
> You can also have to manipulate the set after, for example `set.toggle('A',
> set.has('A-') && set.has('A+'))` (adding A in the set if it contains A- and
> A+, removing it else)
>
> I've just seen this https://github.com/facebook/immutable-js/issues/610
> similar topic.
>
> Last argument for it, Set.prototype.delete returns the number of deleted
> item, Set.prototype.add the amended set, this inconsistency is annoying
> when combining them
>
> 2017-10-29 10:25 GMT+01:00 Naveen Chawla <naveen.chwl at gmail.com>:
>
>> "Nice to have" is good a case as any, since it applies for all features
>> that were ever introduced, and all that ever will be.
>>
>> I'm just curious in what cases this is useful?
>>
>> I'm only asking because I expect a set to represent a single condition
>> for entry/presence in the set e.g. "People", "Dogs", etc. I'm curious under
>> what circumstances the condition for entry/presence changes, that would
>> necessitate a "toggle"? And if that condition changes, then wouldn't that
>> apply across the set, necessitating a transformation to a filtered derived
>> set, not just for a single element? Basically, I'm interested to know, in
>> what circumstances are you trying to do this?
>>
>> On Sun, 29 Oct 2017 at 13:37 dante federici <c.dante.federici at gmail.com>
>> wrote:
>>
>>> What's the case for toggle other than nice to have? In general, I find
>>> toggle actions to be implicitly dependent on existing state -- that is,
>>> anywhere you have toggle you can just as easily say `add(value)` or
>>> `delete(value)`.
>>> _______________________________________________
>>> 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
>>
>>
>
> _______________________________________________
> 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/20171029/d670a39b/attachment.html>


More information about the es-discuss mailing list