ruby / set

This library provides the Set class, which deals with a collection of unordered values with no duplicates.
BSD 2-Clause "Simplified" License
23 stars 13 forks source link

Define `Set#to_ary` #3

Closed marcandre closed 9 months ago

marcandre commented 3 years ago

[Fix #16990]

marcandre commented 3 years ago

A few consequences of this:

# before
[1, 2, Set[3, 4]].join('+') # => "1+2+#<Set: {3, 4}>" (useless)
[Set[1,2]].each{|x, y| p x, y} # => prints "#<Set: {1, 2}>", "nil"
[1, 2] <=> Set[1, 2] # => nil (ok)
[1, 2] <=> Set[2, 1] # => nil

# after
Set.alias_method :to_ary, :to_a
[1, 2, Set[3, 4]].join('+') # => '1+2+3+4' (better I think)
[Set[1,2]].each{|x, y| p x, y} # => prints "1, 2" (better I think)
[1, 2] <=> Set[1, 2] # => 0 (might be problematic?)
[1, 2] <=> Set[2, 1] # => -1

In any case one can't sort a mixed array of arrays and sets currently because Set#<=> in not specialized, but there would not be a way to define it in a way that would work with Arrays anyways.

knu commented 3 years ago

I would much like to go this way, but this could certainly involve a potentially big incompatibility. flatten, flat_map, [set1, set2, *(set3 if cond)], etc.

I'd like to check how it affects Rails and some popular Rails apps before moving forward.

marcandre commented 3 years ago

@knu Did you have a chance to look into this?

knu commented 3 years ago

I cannot estimate how this would cause an impact on private / in-house apps, but at the very least I want to make sure this does not break popular apps like gitlab, mastodon and discourse. What do you think?

marcandre commented 3 years ago

These are good test bases 👍

I you are too worried, then we could wait until Ruby 3.0 is released, then make a separate set gem release with this feature?

knu commented 9 months ago

I'm closing this because of the compatibility issue.