Skip to content

TypedArray: copyWithin/copyAllWithin are typed as returning array, and TypedArray.map can change the element type #9103

Closed

Description

@cristianoc

Found by a bug hunt over the 16 commits from eb087382 to cd641cbc.

copyWithin / copyAllWithin. These are typed as returning array<'a>. Per MDN, TypedArray.prototype.copyWithin returns the typed array it modified:

let v = Int32Array.fromArray([1, 2, 3])
let r = Int32Array.copyAllWithin(v, ~target=1)
let n = r->Array.push(5)

This compiles, then throws TypeError: r.push is not a function. The signatures are in Stdlib_TypedArray.res lines 128 and 145 (the generic module), and #9050 copied them into the Impl functor (lines 600 and 618), which all 11 typed-array modules include. Line 144 also has a duplicated @send @send.

TypedArray.map / mapWithIndex. The generic versions are typed (t<'a>, 'a => 'b) => t<'b> (lines 416 and 420). A typed array's map returns a typed array of the same kind and coerces each result to its element type:

let v: TypedArray.t<int> = %raw(`new Int32Array([1, 2, 3])`)
let s: TypedArray.t<string> = v->TypedArray.map(x => Int.toString(x) ++ "!")
Console.log(s->TypedArray.get(0))   // prints 0, typed option<string>

The Impl versions are typed T.elt => T.elt, which is correct. The generic copyAllWithin and map behave the same at b123b7c3.

🤖 Generated with Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions