Skip to content

Option orders like Rust: None before Some - #31

Merged
nz merged 2 commits into
mainfrom
feat/option-ordering
Aug 7, 2026
Merged

nz merged 2 commits into
mainfrom
feat/option-ordering

Conversation

@nz

@nz nz commented Aug 7, 2026 •

Copy link
Copy Markdown
Member

Fixes #22.

<=> plus Comparable on both containers, matching Rust's derived Ord:

  • Option: None < Some, Somes order by inner value — [Some(2), None(), Some(1)].sort gives [None(), Some(1), Some(2)].
  • Result: Ok < Err (Rust's variant order), same variants order by inner value — [Err(:a), Ok(2), Ok(1)].sort gives [Ok(1), Ok(2), Err(:a)].

One deliberate departure from the issue's suggestion: incomparable operands (non-container other, or inner values that don't mutually compare — e.g. two Errs wrapping exceptions) return nil rather than raising NotComparableError. That is Ruby's <=> contract (1 <=> "x" is nil), and sort/min then raise their standard ArgumentError — a custom raise from <=> itself would make these objects behave unlike every other Ruby operand. Happy to revisit if a clearer sort-time error is worth the unconventional <=>.

Specified as doctests, including the issue's sort and min examples.

<=> previously fell back to Object, returning nil for every pair, so
sort/min/max over Options raised ArgumentError. Implement <=> and
include Comparable: None < Some, Somes order by inner value, and nil
for incomparable operands per Ruby convention.

Fixes #22.
@nz
nz enabled auto-merge August 7, 2026 18:14
Same treatment as Option: implement <=> and include Comparable, with
Ok < Err per Rust's variant ordering, same variants ordering by inner
value, and nil for incomparable operands per Ruby convention.

@lutzcc1 lutzcc1 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting one

@nz
nz merged commit 6ab392d into main Aug 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Option is not orderable — <=> returns nil, sort/min raise (Rust orders None < Some)

2 participants