Skip to content

Per-domain range set types (Int32RangeSet, DateRangeSet, …) replace RangeSet<TRange, T> #275

Done

Description

@CaffeinatedCoder

In 11.0.0 (maintainer, 2026-11-28). It was moved out for a few hours that day, to tag v11.0.0 sooner, and the maintainer put it back: every consumer of a range set gains from it, and it fits only in a major. It ships in 11.0.0-preview.2 (moved there from preview.3 on 2026-11-28; PR #551) and holds the v11.0.0 tag (#147).


Problem

RangeSet<TRange, T> makes every consumer spell both type arguments — RangeSet<DateRange, DateOnly> — although T is fully determined by TRange. C# does not infer type arguments from constraints, so the element type has to be named at every declaration: entity properties, DTOs, method signatures. The From remarks and docs/ranges.md already tell consumers to prefer the collection expression because of it.

The quirk is not local to one declaration. Everything around the set exists because an open generic cannot name a closed type:

  • RangeSet names RangeJsonConverterFactory and registers closed converters through GenericConverters.Register, because an attribute cannot name a converter closed over the set's own type (AGENTS.md, "AOT").
  • The EF translators match members by typeof(RangeSet<,>) and nameof(RangeSet<,>.Union) in ValueRangesMethodCallTranslator, ValueRangesQueryExpressionInterceptor and ValueRangesAggregateMethodCallTranslator, instead of a registry lookup by CLR type as the ranges do.
  • RangeAgg / RangeIntersectAgg over ranges are thirteen per-type overloads for the same reason (docs/architecture.md, "Aggregates").
  • YearMonthRangeSetTypeMapping is a hand-written special case beside the generic ValueRangeSetTypeMapping<TRange, T>.

Change

One sealed set type per domain, built as the value-set family is (StringSet, GuidSet, …):

  • Core: Int32RangeSet, Int64RangeSet, DecimalRangeSet, DateRangeSet, DateTimeRangeSet, DateTimeOffsetRangeSet, TimeRangeSet, Float64RangeSet, Float32RangeSet. NodaTime satellite: LocalDateRangeSet, LocalDateTimeRangeSet, InstantRangeSet, YearMonthRangeSet. The RangeSet suffix keeps the vocabulary the docs, the claim catalogue and area/range-set use; Int32Set (values) against Int32RangeSet (ranges) reads correctly.
  • An IRangeSet<TRange, T> interface and a static-abstract IRangeSetFactory<TSet, TRange, T>, mirroring IValueSet<T> / IValueSetFactory<TSet, T>. The current body of RangeSet.cs moves into an internal engine (Internals/) the thirteen types delegate to, so the algebra stays in one place and a fix to set semantics is one change.
  • Generic producers (Union, Except, Gaps, ToRangeSet, the set aggregates, RangeTypeDefinition<TRange, T>) learn the set type from the range side: a third type parameter on IRangeFactory<TRange, T> or a companion interface with static abstract TSet CreateSet(ReadOnlySpan<TRange>). The infrastructure generics then carry three parameters a consumer never writes. Per-type overloads (the RangeAgg precedent) are the fallback, not the plan.
  • Each set type names its own closed [JsonConverter] and [TypeConverter]; the GenericConverters registration for sets and the factory indirection go. The EF side looks sets up through RangeTypeRegistry by CLR type (RangeSetClrType already exists) and drops the typeof(RangeSet<,>) matches. YearMonthRangeSet stops being the odd one out.
  • RangeSet<TRange, T> and the non-generic RangeSet builder are removed. Int32RangeSet set = [a, b]; keeps working through [CollectionBuilder] on each type.
  • New clone-sweep family (core range sets, nodatime range sets) in clone-waivers.json; claims in claims.json re-pointed; RangeProbes, RangeSetLiftParityTests and the EF discovery pick the types up by reflection.
  • docs/migration.md v10.x section gives the rename as a regex (RangeSet<(\w+)Range, \w+> → $1RangeSet), and the changelog's "what can break you" list carries it.

Why 11.0.0 and not 10.0.0

10.0.0 tags on the commit the gate (#179) passed on 2026-10-19; its changelog opens with "Nothing was removed or renamed", and additions were frozen on 2026-10-18. Epic #147 reserves surface changes for the 11 line, and since #149 (closed unions) turned out non-breaking, this is the one breaking surface change 11.0.0 carries. The cost is that the two lines differ in the set family for the LTS lifetime, so a set-semantics fix on main is a manual port into release/10.x; keeping the algebra in a shared engine keeps that port small. 10.x consumers can alias (using DateRangeSet = CodoMetis.ValueRanges.RangeSet<CodoMetis.ValueRanges.DateRange, System.DateOnly>;) in the meantime; docs/ranges.md should say so.

Footprint (measured 2026-10-19 on main)

Area Mentions of RangeSet<
Shipping source outside RangeSet.cs about 90 across core, EF and both satellites
Test files 87 files, 397 mentions
claims.json 180 lines mention RangeSet

Verification

  • Discovered tests and the clone sweep list what is missing for each new sibling (docs/architecture.md#adding-a-range-or-set-type); dotnet run tools/oracles.cs clones clean or waived.
  • JsonDefaultContractTests, TypeConverterContractTests, OracleSurfaceCoverageTests and OracleCatalogueTests pass with the thirteen types and no RangeSet<,>.
  • Every EF translation test asserts the same SQL as before; ShapeMatrixParityTests, RangeSetLiftParityTests, TemporalStoreParityTests and the aggregate integration tests pass on PostgreSQL 16 and 18.
  • The Native AOT consumer in test/consumer-smoke-test.sh declares and serializes at least one set type per package.
  • ApiCompat suppression file lists every removed member; each is in the changelog's "what can break you" list.

Sequence on the 11 line

After 11.0.0-preview.1 (#103, pure retarget) and preview.2 (#149, closed unions): this is the third step before 11.0.0.

Found-By: maintainer, API review of the 10.0.0 surface

Activity

  1. added this to the 11.0.0 milestone on Oct 19, 2026
  2. added
    enhancement
    breaking
    area/range-set
    package/nodatime
    package/efcore
    package/core
    package/efcore-nodatime
    on Oct 19, 2026
  3. removed this from the 11.0.0 milestone on Nov 28, 2026
  4. added this to the 11.0.0 milestone on Nov 28, 2026
  5. 1 remaining item

  6. CaffeinatedCoder commented on Nov 28, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Design decision: how the generic producers learn the set type

    Decision: a companion interface on the range side, IRangeSetProducer<TRange, T, TSet>, without members. IRangeFactory<TRange, T> keeps its two parameters. The aggregates over sets need one more thing, a one-parameter interface, because the range side can't help them (below).

    Both options from the issue were prototyped on Int32Range (core), LocalDateRange and YearMonthRange (NodaTime) in a scratch copy of main at 9c6b5b1, with the three set types wrapping the current RangeSet<TRange, T>.

    What was measured

    Third parameter on IRangeFactory Companion interface
    a.Union(b) on a union and on a sealed variant, no type arguments compiles, returns Int32RangeSet same
    The same on LocalDateRange and YearMonthRange same same
    Native AOT (PublishAot, osx-arm64, run as a native binary) no trim or AOT warning, same output as the JIT same
    Expression tree for r.Range.Union(b) the receiver stays a MemberAccess typed Int32Range; C# writes no Convert to the interface same
    EF translation with the receiver typed as the interface int4multirange(b."Seats") + int4multirange(@other), and datemultirange(r."BillingPeriod") + datemultirange(@months) for YearMonthRange, either operand order same
    What else changes every where TRange : IRangeFactory<TRange, T> gains a parameter: 85 mentions in src/, 179 with the tests, and the public generics RangeJsonConverter<TRange, T>, RangeVariantJsonConverter<,,>, RangeTypeConverter<,>, RangeVariantTypeConverter<,,> and RangeLowerBoundComparer<,> change arity nothing outside the set family

    So the two are equal at the call site, under AOT and in EF. They differ in how far the break reaches. A third parameter on the factory would break every consumer who names RangeJsonConverter<Int32Range, int> (the AOT section of docs/serialization.md tells them to) or writes a method generic over a range, although neither has anything to do with sets. The companion breaks only what the issue sets out to break.

    In both options the receiver of Union and Except has to be the interface that names TSet, not TRange: C# does not infer from constraints, so extension<TRange, T, TSet>(TRange range) fails with CS0411.

    Why the companion has no CreateSet

    The issue words the companion with static abstract TSet CreateSet(ReadOnlySpan<TRange>). I tried that shape and one without members, where the interface only constrains TSet : IRangeSetFactory<TSet, TRange, T> and a producer calls TSet.From(…). Both work. I'm taking the one without members, for three reasons:

    • An interface with a static abstract member can't be a type argument (CS8920). Without the member, IEnumerable<IRangeSetProducer<TRange, T, TSet>> is legal and infers all three from an IEnumerable<Int32Range>, a Finite[] or an ImmutableArray<Int32Range> (checked). That is the one shape that could later replace the thirteen per-type RangeAgg/RangeIntersectAgg overloads over ranges with two generic methods. This PR leaves those overloads as they are (only their return type changes); it just doesn't close that door.
    • There is one way to build a set (TSet.From), and the thirteen range types implement nothing.
    • The cost is one more where line in code that is generic over a range and its set. Only the library's own generics and tests have such code today.

    The aggregates over sets

    RangeAgg(IEnumerable<RangeSet<TRange, T>>) and RangeIntersectAgg of the same source can't learn anything from the range side: their source is the set. With TSet : IRangeSetFactory<TSet, TRange, T> as the only link, sets.RangeAgg() fails with CS0411, and IEnumerable<IRangeSetFactory<TSet, TRange, T>> is CS8920. Neither option in the issue covers this, so here is what I'm doing instead of falling back to 26 per-type overloads:

    IRangeSetOperations<TSet>, a base of IRangeSetFactory<TSet, TRange, T> holding the members that name the set type alone (Empty, Infinite, Union(TSet), Intersect(TSet), Except(TSet), Complement()). The aggregates become RangeAgg<TSet>(this IEnumerable<TSet?>) where TSet : class, IRangeSetOperations<TSet>. A consumer writes sets.RangeAgg(); the method in the expression tree is closed over Int32RangeSet with a source of IEnumerable<Int32RangeSet>, which is what the aggregate translator already compares against RangeSetClrType. Checked under the JIT and Native AOT, also for YearMonthRangeSet and a source with nulls.

    If you'd rather have the 26 overloads than a third interface, say so and I'll switch; nothing else depends on it.

    The shape that follows

    public interface IRangeSet<TRange, T> : IReadOnlyList<TRange>            // instance view, no set type
    public interface IRangeSetOperations<TSet>                               // the set type alone
    public interface IRangeSetFactory<TSet, TRange, T> : IRangeSetOperations<TSet>, ISpanParsable<TSet>, …
    public interface IRangeSetProducer<TRange, T, TSet>                      // on the range: names its set type
    
    public sealed class Int32RangeSet : IRangeSet<Int32Range, int>, IRangeSetFactory<Int32RangeSet, Int32Range, int>, …
    public abstract partial record Int32Range : IRange<int>, IRangeFactory<Int32Range, int>, IRangeSetProducer<Int32Range, int, Int32RangeSet>

    One PR, in commits that can be reviewed one at a time (engine move, interfaces and types, EF, tests, oracles, docs). If it turns out too large for that I'll propose the split here first.

  7. CaffeinatedCoder commented on Nov 28, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Implemented in #551, open against main (not merged). What the design comment above did not know yet:

    • Generic code binds to the interface members. A query written in a method generic over the set reaches the translators as IRangeSet<TRange, T>.Contains, IRangeSetOperations<TSet>.Union or, for Count, the BCL's IReadOnlyCollection<TRange>.Count. With the generic RangeSet<TRange, T> both forms of a query were the same member. The translators resolve all three now; Count was found untranslated by RangeSetCountParityTests.
    • Set operators in generic query code. |, & and - are static abstract on the interfaces, and C# refuses them in an expression tree written in generic code (CS8927). The methods run as the same SQL. Code that names a set type is unaffected.
    • YearMonthRangeSetTypeMapping stays hand-written. It goes through NpgsqlRange<LocalDate> because YearMonth has no wire form, as YearMonthRangeTypeMapping does. That was never a consequence of the open generic, so this change does not remove it. No translator has a special case for it any more.
    • The engine body is not byte-identical. The public documentation moved to the interfaces, Except(TRange) shapes its pieces through ExceptEngine (the range-level Except now returns a set type), NormalizeSingle names the three bounded shapes and throws for anything else, and the operators stay on the set types. The algebra is the same code.
    • No suppression file. With PackageValidationBaselineFrameworkToIgnore net10.0, pack compares nothing on this line. The PR carries a hand dotnet apicompat run against 10.4.0 instead: 59 differences over the four packages, every one naming RangeSet<…>.

    Open questions are in the PR: which changelog section the ### Breaking lines belong to (Version is still 11.0.0-preview.1, and #546 dates that section), and whether the set aggregates should stay generic over IRangeSetOperations<TSet>.

    Follow-ups: #548 (the alias in the 10.x docs, as this issue asks), #549 (the per-type RangeAgg overloads over ranges could now be generic), #550 (drop the sample's alias when it pins an 11 release).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/range-setbreakingenhancementpackage/corepackage/efcorepackage/efcore-nodatimepackage/nodatime

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions