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
base: nightly
Are you sure you want to change the base?
Conversation
c930369
to
94084a3
Compare
stdlib/src/builtin/simd.mojo
Outdated
Returns: | ||
The value as an integer | ||
""" | ||
constrained[type.is_integral(), "expected integral type"]() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOICE
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 aInt
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
c4962ae
to
8902489
Compare
be29ebc
to
5c0c215
Compare
af3b58f
to
aa76d86
Compare
aa76d86
to
06e9052
Compare
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>
06e9052
to
d4bd2d8
Compare
Fixes #2337
As mentioned in the referenced issue, the
__index__()
method allows types other than the baseInt
to be used when indexing containers without allowing inappropriate types that happen to implementIntable
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 theIndexer
trait.