poc(int): generic range via a Step trait - #3469
Closed
bobzhang wants to merge 2 commits into
Closed
Conversation
Demonstrates that one `range(start, end, step)` can cover multiple numeric types through a minimal `Step` trait (`step_add` / `step_lt`), removing the need for per-type `Int::until` / `Int64::until` / etc. Not a production API — just a feasibility check to open design discussion. Lives as a whitebox test in `int/` for easy iteration; real home would likely be a dedicated `range` package. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
bobzhang
marked this pull request as ready for review
April 24, 2026 10:34
Collaborator
Coverage Report for CI Build 3934Coverage decreased (-0.02%) to 94.876%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
Introduces a new `@range` package with a `Step` trait (`Add + Compare + Default`, plus a static `one()` for the default step) and a single generic `range(start, end, step?, inclusive?) -> Iter[T]` function. One entry point replaces the per-type `Int::until` / `Int64::until` / `Double::until` forms and removes the `0.until(10)` lexer trap where `0.` tokenizes as a `Double` literal. Built-in `Step` impls for `Int`, `Int64`, `UInt`, `UInt64`, `Float`, `Double`. User types opt in by implementing `Step`. Semantics mirror `Int::until` exactly — zero-step yields an empty iterator, negative step descends, inclusive=true includes `end`, and overflow near the type's max is handled the same way. Existing `::until` functions are untouched; deprecation is a follow-up. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
range(start, end, step)that works across numeric types via a minimalSteptrait (step_add/step_lt).fn[T : Step] ...) are enough — we don't need "generics on traits" to unifyInt::until/Int64::until/Double::untilinto one entry point.range(0, 10, 3)is shorter than(0).until(10, step=3)and avoids the0.lexer trap where a bare integer literal followed by.methodis parsed as aDoubleliteral.What's here
int/range_test.mbt(whitebox test):trait Step, impls forIntandInt64, andfn[T : Step] range(...) -> Iter[T], plus tests.int/only so the POC compiles against existing infra; a real home would be a dedicatedrangepackage.Open design questions (for discussion)
step_add+step_lt. To matchInt::untilfully we need negative step andinclusive=true. Options: addstep_gt(or a singlestep_compare -> Int), or keep direction-specific methods.@range.range(0, 10, 3)vs@int.range(...) / @double.range(...)vs prelude export.Int::until/Int64::until/Double::untilonce the generic form lands, or keep them as thin shortcuts?Range[T]struct (not justIter[T]), we can getcontains,length,reverse, slice-indexing — at the cost of a new type in the surface area.Test plan
moon test -p intpasses locally (5256/5256).Double/UInt/Int64/ etc.🤖 Generated with Claude Code