modular icon indicating copy to clipboard operation
modular copied to clipboard

[mojo-stdlib] Create `InlineArray` type

Open lsh opened this issue 1 year ago • 4 comments
trafficstars

This PR creates an InlineArray type that takes any CollectionElement rather than just AnyRegType. See also this thread on Discord.

lsh avatar Apr 13 '24 22:04 lsh

This is super cool @lsh great work!

lattner avatar Apr 19 '24 00:04 lattner

There is a problem with how __refitem__ provides __getitem__ when InlineArray is used as an alias. Maybe since in this case self is immutable in __refitem__? A failing test to demonstrate this is:

alias arr_const = InlineArray[Int, 3](1, 2, 3)
assert_equal(arr_const[0], 1)

A workaround is adding a __getitem__ with immutable self.

fn __getitem__(self, idx: Int) -> Self.ElementType:
    return self.__refitem__(idx)

mikowals avatar Apr 19 '24 07:04 mikowals

There is a problem with how __refitem__ provides __getitem__ when InlineArray is used as an alias. Maybe since in this case self is immutable in __refitem__? A failing test to demonstrate this is:

alias arr_const = InlineArray[Int, 3](1, 2, 3)
assert_equal(arr_const[0], 1)

A workaround is adding a __getitem__ with immutable self.

fn __getitem__(self, idx: Int) -> Self.ElementType:
    return self.__refitem__(idx)

Thanks for calling this out. @lattner would this sort of thing work with the latest top-of-tree mojo based on your changes yesterday with handling the case when both __getitem__ and __refitem__ are present for a type? I worry that we may be able to make it work with the latest public nightly/mojo but it may be rejected on top-of-tree (unreleased). I have not verified that though.

Edit: confirmed this won't compile on top-of-tree internally. You get an error:

stdlib/utils/static_tuple.mojo:389:32: error: cannot implicitly convert 'Reference[ElementType, 0, self, 0]' value to 'ElementType' in return value
        return self.__refitem__(idx)

So, to move forward with this PR, I recommend we file a GitHub issue and (potentially) a commented out failing test case with this GitHub issue number. To make this work at comptime, it can't be solved in the library alone AFAICT — some compiler work is needed. So, we'll handle that internally if that works.

JoeLoser avatar Apr 21 '24 14:04 JoeLoser

✅🟣 This contribution has been merged 🟣✅

Hey @lsh,

As mentioned, we're moving to a new infrastructure for merging contributions to Mojo (we're using a tool called Copybara), and your contribution has now been merged into our internal copy of the Mojo Standard Library. I've added the "merged-internally" label on this PR (FYI @ConnorGray).

The changes in this PR will appear here in the mojo repo nightly branch when we do our next outbound synchronization at the time that the next Mojo nightly is released. That should happen on this coming Monday.

Please let me know if you have any questions or concerns.

JoeLoser avatar Apr 27 '24 14:04 JoeLoser

Closing as this got merged into the latest nightly as https://github.com/modularml/mojo/commit/904ac4ec05dfed0b8f69fb622f8de1d0fcf9bd30

JoeLoser avatar Apr 30 '24 01:04 JoeLoser