[Stdlib] Add by-index `field_at[idx]` to reflection by msaelices · Pull Request #6694 · modular/modular · GitHub
Skip to content

[Stdlib] Add by-index field_at[idx] to reflection#6694

Open
msaelices wants to merge 6 commits into
modular:mainfrom
msaelices:stdlib-reflection-field-type-at
Open

[Stdlib] Add by-index field_at[idx] to reflection#6694
msaelices wants to merge 6 commits into
modular:mainfrom
msaelices:stdlib-reflection-field-type-at

Conversation

@msaelices

@msaelices msaelices commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds reflect[T].field_at[idx], the by-index dual of the existing by-name reflect[T].field[name]. It returns Reflected[FieldT] for the field at index idx, reusing the per-index plumbing field_ref[idx] already relies on.

Resolves #6645.

Note: main renamed the by-name accessor field_type[name]field[name] while this PR was open, so the by-index member here is now spelled field_at[idx] (was field_type_at[idx]) for consistency. Branch merged up to date with main.

Why

Unlike the by-name form (concrete T, name as a StringLiteral), the index form works where only the field index is available, such as inside a comptime for over a struct's fields in generic code. This lets a reflection-derived serializer recover each field's concrete type by index and peel a wrapper like Optional[T] once, instead of enumerating the inner type by hand.

Naming

A separate name (not an overload of field) is required because comptime member aliases cannot be overloaded on parameter type: the compiler rejects two same-named aliases with invalid redefinition. field_at matches the index-keyed siblings field_ref[idx] and field_offset[index=...].

Tests

7 new tests in test_reflect.mojo, all passing (39/39): by-index lookup, parity with the by-name form, composable handle, type annotation, first index, field-type recovery through a generic [T: AnyType] parameter, and peeling an Optional[T] inner type.

A changelog entry is added under Library changes.

Assisted-by: AI

BEGIN_PUBLIC
[Stdlib] Add by-index `field_type_at[idx]` to reflection

Add `reflect[T].field_type_at[idx]`, the by-index dual of the existing
by-name `reflect[T].field_type[name]`. It returns `Reflected[FieldT]` for
the field at `idx`, reusing the same per-index concrete-type plumbing that
`field_ref[idx]` already relies on.

Unlike the by-name form, the index form composes where only the field
index is available, such as inside a `comptime for` over a struct's fields
in generic code, and `T` may be a generic type parameter. This lets a
reflection-derived serializer recover each field's concrete type by index
and peel a wrapper such as `Optional[T]` once, instead of enumerating the
inner type by hand.

A separate name (rather than an overload of `field_type`) is required
because `comptime` member aliases cannot be overloaded on parameter type.

Resolves modular#6645.
END_PUBLIC

Assisted-by: AI
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@github-actions github-actions Bot added mojo-stdlib Tag for issues related to standard library waiting-on-review labels Jun 18, 2026
BEGIN_PUBLIC
[Stdlib] Trim docstrings and comments for field_type_at

Condense the `field_type_at` docstring, the implementation comment, and
the new test docstrings.
END_PUBLIC

Assisted-by: AI
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
BEGIN_PUBLIC
[Stdlib] Add changelog entry for field_type_at

Document the new by-index `reflect[T].field_type_at[idx]` reflection
accessor under Library changes.
END_PUBLIC

Assisted-by: AI
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
@msaelices msaelices marked this pull request as ready for review June 18, 2026 20:30
@msaelices msaelices requested a review from a team as a code owner June 18, 2026 20:30
Copilot AI review requested due to automatic review settings June 18, 2026 20:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new by-index reflection accessor to std.reflection so generic code iterating over struct fields by index can recover each field’s concrete type as a Reflected[FieldT] handle (mirroring the existing by-name field_type[name]).

Changes:

  • Added reflect[T].field_type_at[idx] as a comptime member alias returning Reflected[FieldT] for the field at index idx.
  • Added new stdlib reflection tests covering by-index lookup, parity with by-name lookup, composability, generic usage, and wrapper peeling.
  • Documented the new API in the nightly changelog.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
mojo/stdlib/std/reflection/reflect.mojo Adds field_type_at[idx] and documents it in the reflection API overview list.
mojo/stdlib/test/reflection/test_reflect.mojo Adds tests validating field_type_at behavior and its usefulness in generic/by-index scenarios.
mojo/docs/nightly-changelog.md Adds a Library changes entry documenting the new reflection accessor with an example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 23 to 29

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in 4e483be: the "Member shape" note now lists both field_type[name] and field_type_at[idx] as the type-returning members.

BEGIN_PUBLIC
[Stdlib] Update reflection member-shape note for field_type_at

The "Member shape" note in `Reflected` said `field_type[name]` was the
only type-returning member; update it to also list `field_type_at[idx]`.
END_PUBLIC

Assisted-by: AI
Signed-off-by: Manuel Saelices <msaelices@gmail.com>
…ield-type-at

# Conflicts:
#	mojo/docs/nightly-changelog.md
#	mojo/stdlib/std/reflection/reflect.mojo
@msaelices msaelices changed the title [Stdlib] Add by-index field_type_at[idx] to reflection [Stdlib] Add by-index field_at[idx] to reflection Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mojo-docs mojo-stdlib Tag for issues related to standard library waiting-on-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[stdlib] Reflection: add a by-index field_type[idx] to get a field's concrete type

2 participants