Skip to content

Array-length generics: fn f[N](xs: [T; N]) by Vector's mechanism (D119 Amendment 1) #2311

Closed

Description

@ehartford

Ruled in principle by D119 Amendment 1: array lengths are generic parameters, by the mechanism Vector[N, T] already uses (§4.3d). Today fn total[N](xs: [i32; N]) is rejected: "the length is not a compile-time constant; N is an integer literal or a const (§4.3a)".

Needs its own brief first on one open point (Eric): Vector[N, f32] puts the count before the element and [f32; N] the element first; decide whether the two should agree.

Then: Sema binds N from the argument's array type (incl. multidimensional [f32; N, N], [f32; R, C]); monomorphization per length; tests for trace[N] and transpose[R, C].

Activity

  1. ehartford commented on Oct 12, 2026

    @ehartford
    ContributorAuthor

    Brief on the one open point (D119 Amendment 1: "whether Vector[N, T] and [T; N] should agree on order"), for Eric's ruling before the length-generics stage.

    1. What the others do (checked in .reference/). Each language keeps one order across its array and vector spellings: Rust [T; N] and Simd<T, N> (element first, portable-simd/crates/core_simd/src/vector.rs); Mojo InlineArray[T, N] and SIMD[dtype, size] (element first); Zig [N]T and @Vector(N, T) (count first); Go [N]T (count first, no vector type); Swift [T]/InlineArray<N, T> with SIMD4<Float> (count in the name). No reference puts the count first in one spelling and last in the other.

    2. What the spec says. §4.3a, blessed: [T; N] and [T; d1, d2, …], the element first, dimensions after, printed canonically. §4.3d: Vector[N, T], the count first, with aliases f32x4 etc. that hide the order in nearly every use. The two disagree; nothing in either section says why.

    3. Mission fit. One order is one fewer thing the programmer has to remember, and the array spelling is the one written by hand everywhere (parameters, fields, migrated C); Vector[N, T] is written rarely (29 files in the tree, most through the aliases) and in generic signatures like fn dot[N](a: Vector[N, f32], …), which is exactly where a length generic over [f32; N] will sit beside it. Making Vector follow [T; N] (Vector[f32, 4], aliases unchanged) is the with-y choice: the rule becomes "the element, then its count(s)" everywhere, matching Rust's and Mojo's SIMD. Leaving the disagreement, or flipping arrays to count-first (undoing a ruling made yesterday for migration reasons), both fail law 1 or the spec's stability.

    4. Prediction. Eric rules "they agree; element first, Vector[T, N]; the aliases stay; the rename is one mechanical pass" (80%). Not delegated (D98): it changes a type's spelling in the spec, so the words are his. The implementation afterwards is a Lexer-driven rewrite of the 29 files plus the §4.3d text; the length-generics stage (fn total[N](xs: [T; N]), [f32; R, C], monomorphization per length) follows with the same binding mechanism Vector[N, T] uses today.

  2. added 2 commits that reference this issue on Oct 13, 2026
  3. ehartford commented on Oct 13, 2026

    @ehartford
    ContributorAuthor

    Implemented and merged in #2380 (stack battery GREEN on 975e66f, squash e10eff9): Vector[T, N] element-first everywhere (resolvers, frozen twins, binding walk, validator, all printers; tree renamed in one Lexer-accurate pass), and array lengths as generic parameters by Vector's TY_CONST_INT mechanism — total[N], trace[N], transpose[R, C] in test/behavior/behav_d119a1_array_length_generics.w, conflict and retired-spelling diagnostics in err_d119a1_*. One follow-on found and filed: a bracket literal still takes no generic array demand (#2379, pre-existing on main for concrete-length generic array parameters too).

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions