mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[Feature Request] Canonical Array type.

Open joelflaig opened this issue 10 months ago • 8 comments

Review Mojo's 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.

joelflaig avatar Apr 12 '24 23:04 joelflaig

If by array, you meant array with fixed length, then StaticTuple is just that with a horrible name.

soraros avatar Apr 13 '24 00:04 soraros

Does it work with custom structs?

joelflaig avatar Apr 13 '24 20:04 joelflaig

@joelflaig Yes, and no. It works with register passable types only right now. But better support is on the way, see #2294.

soraros avatar Apr 13 '24 22:04 soraros

@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 avatar Apr 14 '24 20:04 joelflaig

@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

jiex-liu avatar Apr 15 '24 18:04 jiex-liu

Thanks @soraros!

joelflaig avatar Apr 16 '24 20:04 joelflaig

@joelflaig I think you meant "Thanks @LJ-9801". And indeed, thanks, @LJ-9801!

soraros avatar Apr 16 '24 20:04 soraros

haha you are welcome, happy to help!

jiex-liu avatar Apr 16 '24 22:04 jiex-liu

I have closed this, because it looks like there an Array type is coming to the stdlib: https://github.com/modularml/mojo/pull/2294

joelflaig avatar Apr 20 '24 19:04 joelflaig