[NDArray] Add __setitem__ overload for scalar#355
Merged
shivasankarka merged 4 commits intoMay 8, 2026
Merged
Conversation
e9bd259 to
4bba516
Compare
forfudan
approved these changes
May 7, 2026
forfudan
left a comment
Collaborator
There was a problem hiding this comment.
I think maybe we can even create a normal function without dunders if overload is not possible in Mojo.
Collaborator
Author
|
@forfudan I was thinking the same. Should we go with arr.get(Slice(0, 2), 1) -> Get an array
arr.set(Slice(0, 2), 1, val=10.0) -> Set scalar onto the indexed coordinates
arr.set(Slice(0, 2), 1, arr=array) -> Set an array onto the indexed coordinates |
6dec6a9
into
Mojo-Numerics-and-Algorithms-group:pre-0.10
2 checks passed
shivasankarka
added a commit
to shivasankarka/NuMojo
that referenced
this pull request
May 8, 2026
…gorithms-group#355) This PR implements the following methods to support `arr[0:1, 3:4] = 10.0`, ```mojo def __setitem__( mut self, *slices: Variant[Slice, Int], scalar: Scalar[Self.dtype] ) raises: def __setitem__( mut self, *slices: Slice, scalar: Scalar[Self.dtype] ) raises: def _setitem_slice_scalar( mut self, slices: List[Slice], val: Scalar[Self.dtype] ) raises ``` This let's the user do, ```mojo def main() raises: var arr = nm.linspace[nm.f32](0, 9, 10).reshape(Shape(2, 5)) print(arr) arr.__setitem__(0, Slice(0, 3, 1), scalar=Scalar[nm.f32](42.0)) print(arr) ``` ## Note: - Unfortunately overload resolution in Mojo isn't fixed yet. So we have to currently call `__setitem__` explicitly with scalar keyword :(. Hopefully once that's fixed, we should be able to do `arr[0:2, 2] = 10.0`.
shivasankarka
added a commit
to shivasankarka/NuMojo
that referenced
this pull request
May 8, 2026
…gorithms-group#355) This PR implements the following methods to support `arr[0:1, 3:4] = 10.0`, ```mojo def __setitem__( mut self, *slices: Variant[Slice, Int], scalar: Scalar[Self.dtype] ) raises: def __setitem__( mut self, *slices: Slice, scalar: Scalar[Self.dtype] ) raises: def _setitem_slice_scalar( mut self, slices: List[Slice], val: Scalar[Self.dtype] ) raises ``` This let's the user do, ```mojo def main() raises: var arr = nm.linspace[nm.f32](0, 9, 10).reshape(Shape(2, 5)) print(arr) arr.__setitem__(0, Slice(0, 3, 1), scalar=Scalar[nm.f32](42.0)) print(arr) ``` ## Note: - Unfortunately overload resolution in Mojo isn't fixed yet. So we have to currently call `__setitem__` explicitly with scalar keyword :(. Hopefully once that's fixed, we should be able to do `arr[0:2, 2] = 10.0`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements the following methods to support
arr[0:1, 3:4] = 10.0,This let's the user do,
Note:
__setitem__explicitly with scalar keyword :(. Hopefully once that's fixed, we should be able to doarr[0:2, 2] = 10.0.