Project

General

Profile

Actions

Feature #16995

closed

Sets: <=> should be specialized

Added by marcandre (Marc-Andre Lafortune) over 2 years ago. Updated 29 days ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:98970]

Description

This is quite minor, but Set#<=> should be refined.

Reminder: Set defines <, >, etc. as inclusion, but does not have a corresponding <=>:

Set[1] < Set[1, 2] # => true
Set[1] <=> Set[1, 2] # => nil, should be -1
Set[1] <=> Set[2] # => nil, ok, not orderable

The official stated reason for Set to not implement is that some sets are not comparable. That is exactly what nil result type is for IMO. Sets are partically ordered and <=> should reflect that. https://en.wikipedia.org/wiki/Partially_ordered_set

Set[1] < Set[1, 2] # => true
[Set[1], Set[1, 2]].sort # => ArgumentError, should be [Set[1], Set[1, 2]]
[Set[1], Set[2]].sort # => ArgumentError, ok, can't be ordered

This is exactly the same idea as Class, which correctly refines <=>:

Array < Enumerable # => true
Array <=> Enumerable # => -1, ok
[Array, Enumerable].sort # => [Array, Enumerable]
[Array, String].sort # => ArgumentError (comparison of Class with Class failed), ok

Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #16989: Sets: need ♥️Openknu (Akinori MUSHA)Actions
Actions #1

Updated by marcandre (Marc-Andre Lafortune) over 2 years ago

Updated by zverok (Victor Shepelev) 29 days ago

  • Status changed from Open to Closed

Implemented in Ruby 3.0:

Set[1] <=> Set[1, 2]
# => -1 
[Set[1], Set[1, 2]].sort
# => [#<Set: {1}>, #<Set: {1, 2}>] 
Actions

Also available in: Atom PDF

Like0
Like0Like0