mojo
mojo copied to clipboard
[Feature Request] Canonical Array type.
Review Mojo's priorities
- [X] I have read the roadmap and priorities and I believe this request falls within the priorities.
What is your request?
I would like a canonical Array type, working with custom structs to be added to Mojo.
What is your motivation for this change?
Arrays are a fundamental Data Structure, required for more complex programs. Thus, by implementing Canonical Arrays the development of more complex programs in Mojo becomes a lot easier, and the programmer does not have to use the array-like types in the stdlib which apparently are concepted for more or less specific cases. Furthermore neither the array-like types in the stdlib nor any of my custom Arrays work together with custom structs.
Any other details?
Since all my attempts to write a working Array type in pure Mojo did not work with custom structs, I think that a canonical Array may require some compiler magic going on behind the scenes, but if a Array is possible in pure Mojo it will probably be a sort of "wrapper" around the Pointer
type.
If by array, you meant array with fixed length, then StaticTuple
is just that with a horrible name.
Does it work with custom structs?
@joelflaig Yes, and no. It works with register passable types only right now. But better support is on the way, see #2294.
@soraros Thanks, could you provide me with an example for the __setitem__
and __getitem__
methods?
I always get errors when trying to use those methods.
@joelflaig the following should do
var a = StaticTuple[Int32, 3](1, 2, 3)
var one = a[0]
var two = a[1]
a[0] = 10
a[1] = 20
Thanks @soraros!
@joelflaig I think you meant "Thanks @LJ-9801". And indeed, thanks, @LJ-9801!
haha you are welcome, happy to help!
I have closed this, because it looks like there an Array type is coming to the stdlib: https://github.com/modularml/mojo/pull/2294