Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] Parameterize __getitem__ and __setitem__ in stdlib types #2384

Open
wants to merge 1 commit into
base: nightly
Choose a base branch
from

Conversation

bgreni
Copy link

@bgreni bgreni commented Apr 23, 2024

Fixes #2337

As mentioned in the referenced issue, the __index__() method allows types other than the base Int to be used when indexing containers without allowing inappropriate types that happen to implement Intable from being used (such as float point scalars).

Introduce the Indexer trait that requires the type to implement the __index__() method, and change all instances of __getitem__ and __setitem__ to accept any type that has the Indexer trait.

@bgreni bgreni changed the title [stdlib][Draft] Parameterize __getitem__ and __setitem__ in stdlib types [stdlib][Blocked] Parameterize __getitem__ and __setitem__ in stdlib types Apr 23, 2024
Returns:
The value as an integer
"""
constrained[type.is_integral(), "expected integral type"]()
Copy link
Contributor

Choose a reason for hiding this comment

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

NOICE

Copy link
Contributor

Choose a reason for hiding this comment

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

This may not be a good idea, for SIMD at least. This way, SIMD[bool, 1] will be cast to a Int but we might want to index with the original mask (which is size 1).

Copy link
Author

Choose a reason for hiding this comment

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

This may not be a good idea, for SIMD at least. This way, SIMD[bool, 1] will be cast to a Int but we might want to index with the original mask (which is size 1).

Now that you mention it Scalar[bool] actually won't be accepted here, which is inconsistent as I've currently given Bool the Indexer trait, but I was hoping for feedback from the team on whether that was acceptable or not.

We need some support for this in SIMD, however, since it would be expected that one could use any of the integral scalars to index the stdlib containers.

Copy link
Contributor

Choose a reason for hiding this comment

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

I just think we need a more complete design before adding the support to stdlib.

Copy link
Author

Choose a reason for hiding this comment

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

__index__ is an established pattern in Python, and so is indexing with bool so it seems reasonable to me to implement it this way for Mojo.

@bgreni bgreni force-pushed the parameterized-indexing branch 3 times, most recently from c4962ae to 8902489 Compare April 30, 2024 18:33
@bgreni bgreni force-pushed the parameterized-indexing branch 5 times, most recently from be29ebc to 5c0c215 Compare May 6, 2024 02:52
@ematejska ematejska added the mojo-repo Tag all issues with this label label May 6, 2024
@bgreni bgreni force-pushed the parameterized-indexing branch 5 times, most recently from af3b58f to aa76d86 Compare May 8, 2024 05:36
@bgreni bgreni marked this pull request as ready for review May 8, 2024 05:45
@bgreni bgreni requested a review from a team as a code owner May 8, 2024 05:45
@bgreni bgreni changed the title [stdlib][Blocked] Parameterize __getitem__ and __setitem__ in stdlib types [stdlib] Parameterize __getitem__ and __setitem__ in stdlib types May 8, 2024
When indexing stdlib containers we should accept a generic type that calls
on the  __index__ method to allow types other than Int to be used but
doesn't allow Intable types that should not be used for such purposes
(such as Float)

Signed-off-by: Brian Grenier <grenierb96@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mojo-repo Tag all issues with this label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants