Skip to content

Commit

Permalink
Add Array.unsafe_get for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
cknitt committed Oct 19, 2024
1 parent 00274d9 commit 4a20e49
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/array.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
external getUnsafe: (array<'a>, int) => 'a = "%array_unsafe_get"
external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"

external unsafe_get: (array<'a>, int) => 'a = "%array_unsafe_get"

@val external fromIterator: Iterator.t<'a> => array<'a> = "Array.from"
@val external fromArrayLike: Js.Array2.array_like<'a> => array<'a> = "Array.from"
@val
Expand Down
19 changes: 19 additions & 0 deletions runtime/array.resi
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,25 @@ for index in 0 to array->Array.length - 1 {
*/
external getUnsafe: (array<'a>, int) => 'a = "%array_unsafe_get"

/**
`unsafe_get(array, index)` returns the element at `index` of `array`.
This is _unsafe_, meaning it will return `undefined` value if `index` does not exist in `array`.
Use `Array.unsafe_get` only when you are sure the `index` exists (i.e. when using for-loop).
## Examples
```rescript
let array = [1, 2, 3]
for index in 0 to array->Array.length - 1 {
let value = array->Array.unsafe_get(index)
Console.log(value)
}
```
*/
@deprecated("Use getUnsafe instead. This will be removed in v13")
external unsafe_get: (array<'a>, int) => 'a = "%array_unsafe_get"

/**
`setUnsafe(array, index, item)` sets the provided `item` at `index` of `array`.
Expand Down

0 comments on commit 4a20e49

Please sign in to comment.