From: marcandre-ruby-core@... Date: 2020-09-03T00:09:35+00:00 Subject: [ruby-core:99857] [Ruby master Feature#16990] Sets: operators compatibility with Array Issue #16990 has been updated by marcandre (Marc-Andre Lafortune). Eregon (Benoit Daloze) wrote in #note-2: > Isn't `[1].to_set + Set[2]` a good workaround here? Did you mean only for `+`, or for all operators? Take `-` for example... `ary.to_set - set` the `to_set` is wasteful, and I don't want to write it in the first place ���� An important question is: should the result be a `Set`, or an `Array`? In most cases, if there is interoperability, it won't matter that much. I think that `array set` should return an `array`. In my understanding, `array & set` should be a great way to say I have this array, I want to keep only those elements that match the set. It should just work. I'll repeat a usecase which was a bunch of constants in `RuboCop` that were arrays. Changing them to `Set`s would break a bunch of code that does `my_list_of_stuff + OtherClass::STUFF`, for example. ---------------------------------------- Feature #16990: Sets: operators compatibility with Array https://bugs.ruby-lang.org/issues/16990#change-87384 * 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: