feat(match): match any AbstractVector via Indexable[...] and abstract-array heads - #89
Merged
Merged
Conversation
…-array heads Closes #36. `[1, x, 3]` still matches only a concrete `Vector`, preserving the constructor-inverse philosophy (an array literal builds a `Vector`, so it deconstructs a `Vector`). Two new spellings opt into matching any `AbstractVector` (a `view`/`SubArray`, range, ...): - `Indexable[...]` — a reserved keyword recognised by the scanner. - `AbstractVector[...]` / any abstract-array head — matched via `isa`. A `Ref` head `T[...]` is now read as a *container* constraint when `T` is an abstract array type; concrete heads (`Int`, `Vector`, ...) keep the element-type reading (`Vector{T}`), so existing patterns are unchanged. The container/element branch is chosen by `is_container_head`, which is `@assume_effects :foldable` and folds out at compile time (no runtime cost). The shared collection-deconstruction machinery now indexes relative to `firstindex`/`lastindex` instead of a hard-coded `1`, so non-1-based vectors (e.g. `OffsetArray`) deconstruct correctly. For `Vector`/`Tuple`, `firstindex` is `1` and folds away, so there is no perf regression. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #89 +/- ##
==========================================
+ Coverage 91.86% 91.91% +0.05%
==========================================
Files 43 43
Lines 1708 1732 +24
==========================================
+ Hits 1569 1592 +23
- Misses 139 140 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
Benchmark Results (Julia v1)Time benchmarks
Memory benchmarks
|
github-actions Bot
referenced
this pull request
Jul 16, 2026
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.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.
Closes #36.
Motivation
returned
nothingbecause[1, x, 3]checksisa Vector, butview(...)produces aSubArray. As discussed in #36, bare[...]should stayVector-only (an array literal builds aVector, so the pattern deconstructs aVector). This PR adds explicit syntax for matching anyAbstractVectorinstead of widening[...].What's new
[1, x, 3]Vectoronly (unchanged)Int[1, x, 3]Vector{Int}— concrete head still means element type (unchanged)Indexable[1, x, 3]AbstractVector(reserved keyword)AbstractVector[1, x, 3]/ any abstract-array headisathat typeDesign notes
Indexableis a scanner-level reserved keyword →Pattern.Indexable, matchingAbstractVector. No import needed, can't be shadowed — that's its value over spelling outAbstractVector.RefheadT[...]is a container constraint only whenTis an abstract array type (T <: AbstractArray && isabstracttype(unwrap_unionall(T))). Concrete heads (Int,Any,Union{...},Vector,SubArray, ...) keep their existing element-type meaning, so nothing existing breaks.is_container_headisBase.@assume_effects :foldable; theis_container_head(T) ? T : Vector{T}branch const-folds away (verified via@code_typed).firstindex-relative indexing. The shared collection-deconstruction machinery now indexes offfirstindex/lastindexinstead of a hard-coded1, so non-1-based vectors (e.g.OffsetArray) deconstruct correctly. ForVector/Tuple,firstindexis1and folds away — no perf regression (perf-regression testset still passes).Tests
Indexable[...]→Pattern.Indexable, plusshowround-trip.test/match/examples/basic.jl): the exact should vector pattern match any AbstractVector? #36 case,Indexable/AbstractVectoron views/ranges/plain vectors, bare[...]still rejecting aSubArray, typed splats under an abstract container head, and a minimal non-1-basedOffsetVec <: AbstractVectoracross leading/trailing/interior splat shapes.Full suite passes (
data 325,match 140,derive 30,perf 5). Files are blue-formatted.Project.toml/CHANGELOG.mdleft untouched per release-please conventions.🤖 Generated with Claude Code