Issue #16990 has been updated by knu (Akinori MUSHA).


mame (Yusuke Endoh) wrote in #note-9:
> I expect that `ary + set` return a Set, not an Array, unless it raises an exception.
> 
> > Otherwise array += set would turn the variable array to a Set and that would be a surprise.
> 
> It is a surprise if `ary + set` returns a collection object that is ordered and that has multiple instances in its elements.

I will use `array | something` if I mean to deduplicate the result, so `array + something` to me is the way to explicitly say I want to simply concatenate two lists. (should `array + set` be defined)

> To me. `ary += set` looks like `int_val += float`. It is not a surprise to me that it changes the type of `int_val`.

The coercion protocol in Numeric classes works like that not to lose precision.  In that sense, I think Array is to Set as Float is to Integer because a set can be converted to an array without losing information and not the other way around, so you could argue that `set + array` and `array + set` should both return an array when `int + float` and `float + int` both result in a float.

----------------------------------------
Feature #16990: Sets: operators compatibility with Array
https://bugs.ruby-lang.org/issues/16990#change-87440

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
----------------------------------------
We currently have `set <operator> 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: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>