From: eregontp@... Date: 2020-09-03T14:00:08+00:00 Subject: [ruby-core:99874] [Ruby master Feature#16990] Sets: operators compatibility with Array Issue #16990 has been updated by Eregon (Benoit Daloze). Because Array and Set are fundamentally different, I think ensuring both operands have the same type, explicitly or internally, is completely reasonable. I don't expect Array "set" operations to magically know about the Set representation. Having something that is both fast for `include?` and `+` means it needs to keep both a set-like representation and an array-like representation, which is a memory trade-off that `FastArray` in your PRs makes. > my_list_of_stuff + OtherClass::STUFF Which is what type? `set + array` already does `set + array.to_set` and returns a Set (dedup'd elements) `array + set` is not so well defined. Does it do `array + set.to_a` with duplicated elements? And then that makes the return type inconsistent with `set + array`. Doing `array.to_set + set` implicitly doesn't seem nice either as @knu said. I think a few practical examples from RuboCop would help to figure what makes most sense. Maybe having a specialized abstraction like FastArray is what makes most sense for RuboCop if non-set operations are used frequently. ---------------------------------------- Feature #16990: Sets: operators compatibility with Array https://bugs.ruby-lang.org/issues/16990#change-87401 * Author: marcandre (Marc-Andre Lafortune) * Status: Open * Priority: Normal ---------------------------------------- We currently have `set array` work fine: ```ruby Set[1] + [2] # => Set[1, 2] ``` Nothing works in the reverse order: ```ruby [1] + Set[2] # => no implicit conversion of Set into Array # should be: [1] + Set[2] # => [1, 2] ``` #### set-like operators Note that the situation is particularly frustrating for `&`, `|` and `-`. If someone wants to do `ary - set`, one **has** to do `ary - set.to_a` which will, internally, do a `to_set`, so what is happening is `set.to_a.to_set`!! (assuming `ary` is over `SMALL_ARRAY_LEN == 16` size, otherwise it's still doing in `O(ary * set)` instead of `O(ary)`). The same holds with `&` and `|`; see order issue as to why this can *not* (officially) be done any other way. Reminder: ```ruby ary & ary.reverse # => ary Set[*ary] & Set[*ary.reverse] # => Set[*ary.reverse], officially order is indeterminate ``` -- https://bugs.ruby-lang.org/ Unsubscribe: