Skip to content

[NDArray] Add __setitem__ overload for scalar#355

Merged
shivasankarka merged 4 commits into
Mojo-Numerics-and-Algorithms-group:pre-0.10from
shivasankarka:fix_ndarray_indexing
May 8, 2026
Merged

[NDArray] Add __setitem__ overload for scalar#355
shivasankarka merged 4 commits into
Mojo-Numerics-and-Algorithms-group:pre-0.10from
shivasankarka:fix_ndarray_indexing

Conversation

@shivasankarka

@shivasankarka shivasankarka commented May 7, 2026

Copy link
Copy Markdown
Collaborator

This PR implements the following methods to support arr[0:1, 3:4] = 10.0,

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,

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 shivasankarka force-pushed the fix_ndarray_indexing branch from e9bd259 to 4bba516 Compare May 7, 2026 16:27
@forfudan forfudan self-requested a review May 7, 2026 19:12

@forfudan forfudan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think maybe we can even create a normal function without dunders if overload is not possible in Mojo.

@shivasankarka

Copy link
Copy Markdown
Collaborator Author

@forfudan I was thinking the same. Should we go with .get and .set methods? In which case, user can at least do something like

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

@shivasankarka shivasankarka merged commit 6dec6a9 into Mojo-Numerics-and-Algorithms-group:pre-0.10 May 8, 2026
2 checks passed
@shivasankarka shivasankarka deleted the fix_ndarray_indexing branch May 8, 2026 01:54
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`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants