NDArray constructor undermines MultiArray read-only contract
- Dominant language
- Kotlin
- Stars
- 734
- Forks
- 51
- Avg merge
- 9h 52m
- Merged PRs (30d)
- 6
Description
## Problem
`NDArray` accepts an `ImmutableMemoryView` in its constructor and immediately casts it to `MemoryView`:
```kotlin
// multik-core/src/commonMain/kotlin/.../data/NDArray.kt:50
public override val data: MemoryView = data as MemoryView
```
This breaks the `MultiArray` (read-only) → `MutableMultiArray` → `NDArray` hierarchy: any code holding a `MultiArray` reference can observe mutations through shared backing data. The `ImmutableMemoryView` parameter type promises immutability that the implementation doesn't honor — analogous to casting `List` to `MutableList` internally.
## Suggested approach
Either:
- Accept `MemoryView` directly in the constructor (the cast already assumes it), making the contract honest; or
- Drop the `MultiArray` / `MutableMultiArray` split and expose an explicit `readOnly()` wrapper (akin to `Collections.unmodifiableList()`).
Defensive copies are likely too expensive given Multik's view-based slicing semantics.
Contributor guide
Research direction
Start in multik-core/src/commonMain/kotlin/.../data/NDArray.kt at the constructor and inspect the MultiArray, MutableMultiArray, MemoryView, and ImmutableMemoryView relationships. Compare the two suggested contract changes and trace how view-based slicing depends on them. Done means the public read-only contract no longer permits mutation through NDArray's shared backing data without introducing an unsuitable defensive copy.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- kotlin
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100