Skip to content

[stdlib] Make InlineArray conform to Writable, Stringable and Representable#5693

Closed
msaelices wants to merge 13 commits into
modular:mainfrom
msaelices:inline-array-writable
Closed

[stdlib] Make InlineArray conform to Writable, Stringable and Representable#5693
msaelices wants to merge 13 commits into
modular:mainfrom
msaelices:inline-array-writable

Conversation

@msaelices
Copy link
Copy Markdown
Contributor

@msaelices msaelices commented Dec 20, 2025

Adds explicit trait conformance to InlineArray for Writable, Stringable, and Representable traits.

  • Implements write_to() with compile-time constraint checking
  • Adds __str__() and __repr__() methods
  • String format: InlineArray[ElementType, size](elem1, elem2, ...)

…ntable

This change adds explicit trait conformance to InlineArray for:
- Writable: via write_to() method
- Stringable: via __str__() method
- Representable: via __repr__() method

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Consolidates the string representation tests into the main InlineArray
test file to keep all tests in one place.

Signed-off-by: Manuel Saelices <msaelices@gmail.com>
Copilot AI review requested due to automatic review settings December 20, 2025 21:09
@msaelices msaelices requested a review from a team as a code owner December 20, 2025 21:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds trait conformance for Writable, Stringable, and Representable to the InlineArray type, enabling consistent string representation capabilities similar to other collection types like List and Dict.

Key changes:

  • Implements write_to() method with compile-time constraint checking for Representable elements
  • Adds __str__() and __repr__() methods that format arrays as InlineArray[ElementType, size](elem1, elem2, ...)
  • Comprehensive test coverage for the new functionality with various element types

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
mojo/stdlib/std/collections/inline_array.mojo Added trait conformances to struct declaration and implemented write_to(), __str__(), and __repr__() methods with proper constraint checking
mojo/stdlib/test/collections/test_inline_array.mojo Added three new test functions covering string representation, multiple element types, and the write_to() method
mojo/docs/changelog.md Updated changelog to document the new trait conformances for InlineArray
Comments suppressed due to low confidence (1)

mojo/stdlib/std/collections/inline_array.mojo:91

  • The struct-level docstring should be updated to mention that InlineArray now conforms to Writable, Stringable, and Representable traits. This documentation is important for users to understand the full capabilities of the type, especially since these new trait conformances enable string representation and formatting operations.
    """A fixed-size sequence of homogeneous elements where size is a constant
    expression.

    InlineArray provides a fixed-size array implementation with compile-time
    size checking. The array size is determined at compile time and cannot be
    changed. Elements must implement the `Copyable` trait.

    Parameters:
        ElementType: The type of the elements in the array. Must implement
            `Copyable` trait.
        size: The size of the array. Must be a positive integer constant.

    Examples:

    ```mojo
    # Create array of 3 integers
    var arr = InlineArray[Int, 3](1, 2, 3)

    # Create array filled with value
    var filled = InlineArray[Int, 5](fill=42)

    # Access elements
    print(arr[0])  # Prints 1
    ```
    """

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

Comment on lines +661 to +678
fn __str__(self) -> String:
"""Returns a string representation of the InlineArray.

Returns:
A string representation of the array.
"""
output = String()
self.write_to(output)
return output^

@always_inline
fn __repr__(self) -> String:
"""Returns a string representation of the InlineArray.

Returns:
A string representation of the array.
"""
return self.__str__()
Copy link

Copilot AI Dec 20, 2025

Choose a reason for hiding this comment

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

The documentation for __str__() and __repr__() methods should include a Constraints section to document that ElementType must conform to Representable, similar to the write_to() method. This is important because these methods will fail at compile time if the constraint is not met.

Copilot uses AI. Check for mistakes.
Comment on lines +341 to +362
def test_different_types():
"""Test with different element types."""
# Test with String
var string_array = InlineArray[String, 2]("hello", "world")
var str_result = string_array.__str__()
assert_equal(str_result, "InlineArray[String, 2]('hello', 'world')")

# Test with single element
var single = InlineArray[Int, 1](42)
var single_str = single.__str__()
assert_equal(single_str, "InlineArray[Int, 1](42)")

# Test with default values
var default_array = InlineArray[Int, 3](fill=0)
var default_str = default_array.__str__()
assert_equal(default_str, "InlineArray[Int, 3](0, 0, 0)")

# Test with filled array
var filled_array = InlineArray[Int, 4](fill=99)
var filled_str = filled_array.__str__()
assert_equal(filled_str, "InlineArray[Int, 4](99, 99, 99, 99)")

Copy link

Copilot AI Dec 20, 2025

Choose a reason for hiding this comment

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

Missing test coverage for the edge case of an empty InlineArray (size=0). While InlineArray allows size >= 0, there are no tests verifying that __str__(), __repr__(), and write_to() methods work correctly with an empty array. The expected output should be "InlineArrayElementType, 0" with no elements listed inside the parentheses.

Copilot uses AI. Check for mistakes.
@JoeLoser
Copy link
Copy Markdown
Member

JoeLoser commented Jan 6, 2026

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Jan 6, 2026
@modularbot
Copy link
Copy Markdown
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the main branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally Merged externally in public mojo repo labels Jan 8, 2026
@modularbot
Copy link
Copy Markdown
Collaborator

Landed in 063b891! Thank you for your contribution 🎉

@modularbot modularbot closed this in 063b891 Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants