Kotlin / Kotlin/multik

NDArray constructor undermines MultiArray read-only contract

Open
#324 0 comments 0 reactions 0 assignees View on GitHub
ai-generated api ndarray refactor
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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.