{{ message }}
[Stdlib] Add by-index field_at[idx] to reflection#6694
Open
msaelices wants to merge 6 commits into
Open
Conversation
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>
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>
Contributor
There was a problem hiding this comment.
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 acomptimemember alias returningReflected[FieldT]for the field at indexidx. - 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.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
23
to
29
Contributor
Author
There was a problem hiding this comment.
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
field_type_at[idx] to reflectionfield_at[idx] to reflection
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
Adds
reflect[T].field_at[idx], the by-index dual of the existing by-namereflect[T].field[name]. It returnsReflected[FieldT]for the field at indexidx, reusing the per-index plumbingfield_ref[idx]already relies on.Resolves #6645.
Why
Unlike the by-name form (concrete
T, name as aStringLiteral), the index form works where only the field index is available, such as inside acomptime forover 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 likeOptional[T]once, instead of enumerating the inner type by hand.Naming
A separate name (not an overload of
field) is required becausecomptimemember aliases cannot be overloaded on parameter type: the compiler rejects two same-named aliases withinvalid redefinition.field_atmatches the index-keyed siblingsfield_ref[idx]andfield_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 anOptional[T]inner type.A changelog entry is added under Library changes.
Assisted-by: AI