Skip to content

NDArray constructor undermines MultiArray read-only contract #324

Description

@devcrocod

Problem

NDArray accepts an ImmutableMemoryView<T> in its constructor and immediately casts it to MemoryView<T>:

// multik-core/src/commonMain/kotlin/.../data/NDArray.kt:50
public override val data: MemoryView<T> = data as MemoryView<T>

This breaks the MultiArray (read-only) → MutableMultiArrayNDArray 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<T> to MutableList<T> internally.

Suggested approach

Either:

  • Accept MemoryView<T> 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai-generatedIssues or pull requests created fully or partially with the help of AI toolsapiCommon apindarrayCore NDArray data structure, shapes, indexing, broadcastingrefactorInternal code improvements (no behavior change)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions