[stdlib] Make InlineArray conform to Writable, Stringable and Representable#5693
[stdlib] Make InlineArray conform to Writable, Stringable and Representable#5693msaelices wants to merge 13 commits into
Conversation
…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>
There was a problem hiding this comment.
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 forRepresentableelements - Adds
__str__()and__repr__()methods that format arrays asInlineArray[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, andRepresentabletraits. 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.
| 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__() |
There was a problem hiding this comment.
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.
| 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)") | ||
|
|
There was a problem hiding this comment.
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.
|
!sync |
|
✅🟣 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. |
|
Landed in 063b891! Thank you for your contribution 🎉 |
Adds explicit trait conformance to InlineArray for Writable, Stringable, and Representable traits.
write_to()with compile-time constraint checking__str__()and__repr__()methodsInlineArray[ElementType, size](elem1, elem2, ...)