Skip to content

Commit 4a20e49

Browse files
committed
Add Array.unsafe_get for compatibility
1 parent 00274d9 commit 4a20e49

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

runtime/array.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
external getUnsafe: (array<'a>, int) => 'a = "%array_unsafe_get"
44
external setUnsafe: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
55

6+
external unsafe_get: (array<'a>, int) => 'a = "%array_unsafe_get"
7+
68
@val external fromIterator: Iterator.t<'a> => array<'a> = "Array.from"
79
@val external fromArrayLike: Js.Array2.array_like<'a> => array<'a> = "Array.from"
810
@val

runtime/array.resi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,25 @@ for index in 0 to array->Array.length - 1 {
862862
*/
863863
external getUnsafe: (array<'a>, int) => 'a = "%array_unsafe_get"
864864

865+
/**
866+
`unsafe_get(array, index)` returns the element at `index` of `array`.
867+
868+
This is _unsafe_, meaning it will return `undefined` value if `index` does not exist in `array`.
869+
870+
Use `Array.unsafe_get` only when you are sure the `index` exists (i.e. when using for-loop).
871+
872+
## Examples
873+
```rescript
874+
let array = [1, 2, 3]
875+
for index in 0 to array->Array.length - 1 {
876+
let value = array->Array.unsafe_get(index)
877+
Console.log(value)
878+
}
879+
```
880+
*/
881+
@deprecated("Use getUnsafe instead. This will be removed in v13")
882+
external unsafe_get: (array<'a>, int) => 'a = "%array_unsafe_get"
883+
865884
/**
866885
`setUnsafe(array, index, item)` sets the provided `item` at `index` of `array`.
867886

0 commit comments

Comments
 (0)