Skip to content

feat: the non-generic IComparable on every range union and RangeSet - #230

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
feat/nongeneric-icomparable
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
feat/nongeneric-icomparable

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #225

Stacked on #229 (#222): the new contract test uses that PR's RangeCensus to show it reached every union. Merge #229 first; this PR's base then moves to main.

Problem

The range unions implemented IComparable<TRange> (#39) but not the non-generic IComparable. So List<IRange<int>>.Sort(), ArrayList.Sort() and Comparer.Default threw "At least one object must implement IComparable", although the docs say List.Sort() works with no comparer.

Change

  • IRangeFactory<TRange, T> also extends IComparable. Each of the 13 unions implements it explicitly, int IComparable.CompareTo(object? other), and so does RangeSet. The public surface keeps one CompareTo, and EF Core still binds the generic one in a query.
  • RangeOrder.CompareObject. It delegates to the same order: null sorts first, as in the generic form. An argument that is not the same type throws ArgumentException naming both types ("Object must be of type Int32Range, not DecimalRange.").
  • docs/ranges.md#sort-ranges: the sorting sentence now covers List<IRange<T>> and ArrayList, and states the ArgumentException. The multirange sentence names IComparable too.
  • Claim R43. fix: CompareTo against a null range in a query returns the rows memory returns #227 takes R42, so this one takes R43 to avoid a duplicate number. The existing 10.0.0 Added lines (root, core, NodaTime) now say IComparable<TRange> and IComparable. This amends a 10.0.0 addition, so it adds no new line.

Tests

  • NonGenericComparableContractTests (conventions, discovered). For every union, the non-generic CompareTo gives the generic sign for every ordered pair of eight shapes, and likewise for their one-element RangeSets plus {}. It is positive for null. It throws ArgumentException naming the type for another range type, the element value and a string, and the set form throws for a range. List<IRange<T>>.Sort() and ArrayList.Sort() sort as Order() does. RangeCensus confirms all 13 unions were reached.
  • Proved by reverting. With src/ back at main (no non-generic interface), it fails with EmptyRange does not implement the non-generic IComparable. The casts go through object, so the revert fails at run time rather than at compile time. With a foreign argument ordered (_ => 1) instead of refused, it fails with Expected exception of exact type ArgumentException.
  • The unit, NodaTime, EF, conventions and ASP.NET Core tests and the full integration suite pass locally.

🤖 Generated with Claude Code

The unions implemented IComparable<TRange> only, so List<IRange<T>>.Sort(), ArrayList.Sort() and
Comparer.Default threw "At least one object must implement IComparable". IRangeFactory now also
requires IComparable, and each of the 13 unions and RangeSet implements it explicitly, so the
public surface keeps one CompareTo and EF Core still binds the generic one. It delegates to the
same range order through RangeOrder.CompareObject: null sorts first, and an argument of another
type throws ArgumentException naming both types.

NonGenericComparableContractTests discovers every union, checks that the non-generic answer
matches the generic one for every ordered shape pair of the range and of its RangeSet, that a
foreign argument throws, and that List<IRange<T>> and ArrayList sort as Order() does; it fails
with the interface removed and with a foreign argument ordered instead of refused.

Closes #225

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request area/ranges Range unions, variants and the shape engines package/nodatime Affects CodoMetis.ValueRanges.NodaTime package/core Affects CodoMetis.ValueRanges stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 18, 2026
Base automatically changed from test/union-discovery-floors to main October 18, 2026 19:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ranges Range unions, variants and the shape engines enhancement New feature or request package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime stacked Base is another open PR; merge in order, CI does not run until it targets main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Range unions implement IComparable<T> but not IComparable, so non-generic sorts throw

1 participant