Skip to content

Commit 118294a

Browse files
authored
Use more optional args in stdlib and deprecate some functions (#7730)
* Use more optional args in stdlib and deprecate some functions * CHANGELOG
1 parent 39e90c7 commit 118294a

20 files changed

+258
-192
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Apply heuristic to suggest using JSX fragments where we guess that might be what the user wanted. https://github.com/rescript-lang/rescript/pull/7714
1818
- Show deprecation warnings for `bs-dependencies` etc. for local dependencies only. https://github.com/rescript-lang/rescript/pull/7724
1919
- Add check for minimum required node version. https://github.com/rescript-lang/rescript/pull/7723
20+
- Use more optional args in stdlib and deprecate some functions. https://github.com/rescript-lang/rescript/pull/7730
2021

2122
#### :bug: Bug fix
2223

runtime/Stdlib_Array.res

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ external unsafe_get: (array<'a>, int) => 'a = "%array_unsafe_get"
1313
@val
1414
external fromArrayLikeWithMap: (arrayLike<'a>, 'a => 'b) => array<'b> = "Array.from"
1515

16-
@send external fillAll: (array<'a>, 'a) => unit = "fill"
16+
@deprecated("Use `fill` instead") @send external fillAll: (array<'a>, 'a) => unit = "fill"
1717

18-
@send external fillToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
18+
@deprecated("Use `fill` instead") @send
19+
external fillToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
1920

20-
@send external fill: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
21+
@send external fill: (array<'a>, 'a, ~start: int=?, ~end: int=?) => unit = "fill"
2122

2223
let make = (~length, x) =>
2324
if length <= 0 {
@@ -83,13 +84,14 @@ let compare = (a, b, cmp) => {
8384
: compareFromIndex(a, b, 0, cmp, lenA)
8485
}
8586

86-
@send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
87+
@deprecated("Use `copyWithin` instead") @send
88+
external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
8789

88-
@send
90+
@deprecated("Use `copyWithin` instead") @send
8991
external copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a> = "copyWithin"
9092

9193
@send
92-
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin"
94+
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int=?) => array<'a> = "copyWithin"
9395

9496
@send external pop: array<'a> => option<'a> = "pop"
9597

@@ -124,13 +126,14 @@ external removeInPlace: (array<'a>, int, @as(1) _) => unit = "splice"
124126

125127
@send external includes: (array<'a>, 'a) => bool = "includes"
126128

127-
@send external indexOf: (array<'a>, 'a) => int = "indexOf"
129+
@send external indexOf: (array<'a>, 'a, ~from: int=?) => int = "indexOf"
128130
let indexOfOpt = (arr, item) =>
129131
switch arr->indexOf(item) {
130132
| -1 => None
131133
| index => Some(index)
132134
}
133-
@send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"
135+
@deprecated("Use `indexOf` instead") @send
136+
external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"
134137

135138
@send external join: (array<string>, string) => string = "join"
136139

@@ -142,16 +145,19 @@ external joinWith: (array<string>, string) => string = "join"
142145
@deprecated("Use `joinUnsafe` instead") @send
143146
external joinWithUnsafe: (array<'a>, string) => string = "join"
144147

145-
@send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf"
148+
@send external lastIndexOf: (array<'a>, 'a, ~from: int=?) => int = "lastIndexOf"
146149
let lastIndexOfOpt = (arr, item) =>
147150
switch arr->lastIndexOf(item) {
148151
| -1 => None
149152
| index => Some(index)
150153
}
151-
@send external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf"
154+
@deprecated("Use `lastIndexOf` instead") @send
155+
external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf"
156+
157+
@send external slice: (array<'a>, ~start: int=?, ~end: int=?) => array<'a> = "slice"
158+
@deprecated("Use `slice` instead") @send
159+
external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
152160

153-
@send external slice: (array<'a>, ~start: int, ~end: int) => array<'a> = "slice"
154-
@send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
155161
@send external copy: array<'a> => array<'a> = "slice"
156162

157163
@send external sort: (array<'a>, ('a, 'a) => Stdlib_Ordering.t) => unit = "sort"

runtime/Stdlib_Array.resi

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,32 @@ someArray->Array.length == 2
8181
external length: array<'a> => int = "length"
8282

8383
// TODO: Docs
84-
@send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
84+
@deprecated("Use `copyWithin` instead") @send
85+
external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin"
8586

8687
// TODO: Docs
87-
@send
88+
@deprecated("Use `copyWithin` instead") @send
8889
external copyWithinToEnd: (array<'a>, ~target: int, ~start: int) => array<'a> = "copyWithin"
8990

90-
// TODO: Docs
91+
/**
92+
`copyWithin(array, ~target, ~start, ~end)` copies the sequence of array elements within the array to the position starting at `target`. The copy is taken from the index positions `start` to `end`.
93+
94+
Beware this will *mutate* the array.
95+
96+
See [`Array.copyWithin`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin) on MDN.
97+
98+
## Examples
99+
100+
```rescript
101+
let myArray = [1, 2, 3, 4, 5]
102+
myArray->Array.copyWithin(~target=0, ~start=3) == [4, 5, 3, 4, 5]
103+
104+
let myArray = [1, 2, 3, 4, 5]
105+
myArray->Array.copyWithin(~target=1, ~start=3, ~end=4) == [1, 4, 3, 4, 5]
106+
```
107+
*/
91108
@send
92-
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int) => array<'a> = "copyWithin"
109+
external copyWithin: (array<'a>, ~target: int, ~start: int, ~end: int=?) => array<'a> = "copyWithin"
93110

94111
/**
95112
`fillAll(array, value)` fills the entire `array` with `value`.
@@ -106,7 +123,7 @@ myArray->Array.fillAll(9)
106123
myArray == [9, 9, 9, 9]
107124
```
108125
*/
109-
@send
126+
@deprecated("Use `fill` instead") @send
110127
external fillAll: (array<'a>, 'a) => unit = "fill"
111128

112129
/**
@@ -124,7 +141,7 @@ myArray->Array.fillToEnd(9, ~start=1)
124141
myArray == [1, 9, 9, 9]
125142
```
126143
*/
127-
@send
144+
@deprecated("Use `fill` instead") @send
128145
external fillToEnd: (array<'a>, 'a, ~start: int) => unit = "fill"
129146

130147
/**
@@ -139,13 +156,18 @@ See [`Array.fill`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer
139156
```rescript
140157
let myArray = [1, 2, 3, 4]
141158
142-
myArray->Array.fill(9, ~start=1, ~end=3)
159+
myArray->Array.fill(9)
160+
myArray == [9, 9, 9, 9]
161+
162+
myArray->Array.fill(0, ~start=1)
163+
myArray == [9, 0, 0, 0]
143164
144-
myArray == [1, 9, 9, 4]
165+
myArray->Array.fill(5, ~start=1, ~end=3)
166+
myArray == [9, 5, 5, 0]
145167
```
146168
*/
147169
@send
148-
external fill: (array<'a>, 'a, ~start: int, ~end: int) => unit = "fill"
170+
external fill: (array<'a>, 'a, ~start: int=?, ~end: int=?) => unit = "fill"
149171

150172
/**
151173
`pop(array)` removes the last item from `array` and returns it.
@@ -416,9 +438,9 @@ See [`Array.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/R
416438
external includes: (array<'a>, 'a) => bool = "includes"
417439

418440
/**
419-
`indexOf(array, item)` returns the index of the provided `item` in `array`. Uses [strict check for equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) when comparing items.
441+
`indexOf(array, item, ~from)` returns the index of the provided `item` in `array`, starting the search at `from`. Uses [strict check for equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) when comparing items.
420442
421-
Returns `-1` if the item doesn not exist. Check out `Array.indexOfOpt` for a version that returns `None` instead of `-1` if the item does not exist.
443+
Returns `-1` if the item isn't found. Check out `Array.indexOfOpt` for a version that returns `None` instead of `-1` if the item does not exist.
422444
423445
See [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) on MDN.
424446
@@ -427,12 +449,13 @@ See [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re
427449
```rescript
428450
[1, 2]->Array.indexOf(2) == 1
429451
[1, 2]->Array.indexOf(3) == -1
452+
[1, 2, 1, 2]->Array.indexOf(2, ~from=2) == 3
430453
431454
[{"language": "ReScript"}]->Array.indexOf({"language": "ReScript"}) == -1 // -1, because of strict equality
432455
```
433456
*/
434457
@send
435-
external indexOf: (array<'a>, 'a) => int = "indexOf"
458+
external indexOf: (array<'a>, 'a, ~from: int=?) => int = "indexOf"
436459

437460
/**
438461
`indexOfOpt(array, item)` returns an option of the index of the provided `item` in `array`. Uses [strict check for equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) when comparing items.
@@ -448,7 +471,8 @@ See [`Array.indexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Re
448471
```
449472
*/
450473
let indexOfOpt: (array<'a>, 'a) => option<int>
451-
@send external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"
474+
@deprecated("Use `indexOf` instead") @send
475+
external indexOfFrom: (array<'a>, 'a, int) => int = "indexOf"
452476

453477
/**
454478
`join(array, separator)` produces a string where all items of `array` are printed, separated by `separator`. Array items must be strings, to join number or other arrays, use `joinUnsafe`. Under the hood this will run JavaScript's `toString` on all the array items.
@@ -501,9 +525,27 @@ external joinUnsafe: (array<'a>, string) => string = "join"
501525
*/
502526
@deprecated("Use `joinUnsafe` instead") @send
503527
external joinWithUnsafe: (array<'a>, string) => string = "join"
504-
@send external lastIndexOf: (array<'a>, 'a) => int = "lastIndexOf"
528+
/**
529+
`lastIndexOf(array, item, ~from)` returns the last index of the provided `item` in `array`, searching backwards from `from`. Uses [strict check for equality](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality) when comparing items.
530+
531+
Returns `-1` if the item isn't found. Check out `Array.lastIndexOfOpt` for a version that returns `None` instead of `-1` if the item does not exist.
532+
533+
See [`Array.lastIndexOf`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf) on MDN.
534+
535+
## Examples
536+
537+
```rescript
538+
[1, 2, 1, 2]->Array.lastIndexOf(2) == 3
539+
[1, 2]->Array.lastIndexOf(3) == -1
540+
[1, 2, 1, 2]->Array.lastIndexOf(2, ~from=2) == 1
541+
542+
[{"language": "ReScript"}]->Array.lastIndexOf({"language": "ReScript"}) == -1 // -1, because of strict equality
543+
```
544+
*/
545+
@send external lastIndexOf: (array<'a>, 'a, ~from: int=?) => int = "lastIndexOf"
505546
let lastIndexOfOpt: (array<'a>, 'a) => option<int>
506-
@send external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf"
547+
@deprecated("Use `lastIndexOf` instead") @send
548+
external lastIndexOfFrom: (array<'a>, 'a, int) => int = "lastIndexOf"
507549

508550
/**
509551
`slice(array, ~start, ~end)` creates a new array of items copied from `array` from `start` until (but not including) `end`.
@@ -514,10 +556,12 @@ See [`Array.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
514556
515557
```rescript
516558
[1, 2, 3, 4]->Array.slice(~start=1, ~end=3) == [2, 3]
559+
[1, 2, 3, 4]->Array.slice(~start=1) == [2, 3, 4]
560+
[1, 2, 3, 4]->Array.slice == [1, 2, 3, 4]
517561
```
518562
*/
519563
@send
520-
external slice: (array<'a>, ~start: int, ~end: int) => array<'a> = "slice"
564+
external slice: (array<'a>, ~start: int=?, ~end: int=?) => array<'a> = "slice"
521565

522566
/**
523567
`sliceToEnd(array, start)` creates a new array from `array`, with all items from `array` starting from `start`.
@@ -530,7 +574,7 @@ See [`Array.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe
530574
[1, 2, 3, 4]->Array.sliceToEnd(~start=1) == [2, 3, 4]
531575
```
532576
*/
533-
@send
577+
@deprecated("Use `slice` instead") @send
534578
external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice"
535579
/**
536580
`copy(array)` makes a copy of the array with the items in it, but does not make copies of the items themselves.

runtime/Stdlib_BigInt64Array.res

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,42 @@ external fromArray: array<bigint> => t = "BigInt64Array"
1616

1717
/** `fromBuffer` creates a `BigInt64Array` from an `ArrayBuffer.t`. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
1818
19-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
19+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
2020
*/
2121
@new
22-
external fromBuffer: Stdlib_ArrayBuffer.t => t = "BigInt64Array"
22+
external fromBuffer: (Stdlib_ArrayBuffer.t, ~byteOffset: int=?, ~length: int=?) => t =
23+
"BigInt64Array"
2324

2425
/** `fromBufferToEnd` creates a `BigInt64Array` from an `ArrayBuffer.t`, starting at a particular offset and continuing through to the end. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
2526
26-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
27+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
2728
*/
28-
@new
29+
@deprecated("Use `fromBuffer` instead") @new
2930
external fromBufferToEnd: (Stdlib_ArrayBuffer.t, ~byteOffset: int) => t = "BigInt64Array"
3031

3132
/** `fromBufferWithRange` creates a `BigInt64Array` from an `ArrayBuffer.t`, starting at a particular offset and consuming `length` **bytes**. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
3233
33-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
34+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
3435
*/
35-
@new
36+
@deprecated("Use `fromBuffer` instead") @new
3637
external fromBufferWithRange: (Stdlib_ArrayBuffer.t, ~byteOffset: int, ~length: int) => t =
3738
"BigInt64Array"
3839

3940
/** `fromLength` creates a zero-initialized `BigInt64Array` to hold the specified count of numbers; this is **not** a byte length. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array/BigInt64Array)
4041
41-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
42+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
4243
*/
4344
@new
4445
external fromLength: int => t = "BigInt64Array"
4546

4647
/** `fromArrayLikeOrIterable` creates a `BigInt64Array` from an array-like or iterable object. See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
4748
*/
4849
@val
49-
external fromArrayLikeOrIterable: 'a => t = "BigInt64Array.from"
50+
external fromArrayLikeOrIterable: ('a, ~map: ('b, int) => bigint=?) => t = "BigInt64Array.from"
5051

5152
/** `fromArrayLikeOrIterableWithMap` creates a `BigInt64Array` from an array-like or iterable object and applies the mapping function to each item. The mapping function expects (value, index). See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
5253
*/
53-
@val
54+
@deprecated("Use `fromArrayLikeOrIterable` instead") @val
5455
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => bigint) => t = "BigInt64Array.from"
5556

5657
/**

runtime/Stdlib_BigUint64Array.res

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,42 @@ external fromArray: array<bigint> => t = "BigUint64Array"
1616

1717
/** `fromBuffer` creates a `BigUint64Array` from an `ArrayBuffer.t`. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array/BigUint64Array)
1818
19-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
19+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
2020
*/
2121
@new
22-
external fromBuffer: Stdlib_ArrayBuffer.t => t = "BigUint64Array"
22+
external fromBuffer: (Stdlib_ArrayBuffer.t, ~byteOffset: int=?, ~length: int=?) => t =
23+
"BigUint64Array"
2324

2425
/** `fromBufferToEnd` creates a `BigUint64Array` from an `ArrayBuffer.t`, starting at a particular offset and continuing through to the end. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array/BigUint64Array)
2526
26-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
27+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
2728
*/
28-
@new
29+
@deprecated("Use `fromBuffer` instead") @new
2930
external fromBufferToEnd: (Stdlib_ArrayBuffer.t, ~byteOffset: int) => t = "BigUint64Array"
3031

3132
/** `fromBufferWithRange` creates a `BigUint64Array` from an `ArrayBuffer.t`, starting at a particular offset and consuming `length` **bytes**. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array/BigUint64Array)
3233
33-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
34+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
3435
*/
35-
@new
36+
@deprecated("Use `fromBuffer` instead") @new
3637
external fromBufferWithRange: (Stdlib_ArrayBuffer.t, ~byteOffset: int, ~length: int) => t =
3738
"BigUint64Array"
3839

3940
/** `fromLength` creates a zero-initialized `BigUint64Array` to hold the specified count of numbers; this is **not** a byte length. See [TypedArray constructor on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array/BigUint64Array)
4041
41-
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
42+
**Note:** This is a potentially unsafe operation. Ensure the buffer is large enough and only accessed within its bounds.
4243
*/
4344
@new
4445
external fromLength: int => t = "BigUint64Array"
4546

4647
/** `fromArrayLikeOrIterable` creates a `BigUint64Array` from an array-like or iterable object. See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
4748
*/
4849
@val
49-
external fromArrayLikeOrIterable: 'a => t = "BigUint64Array.from"
50+
external fromArrayLikeOrIterable: ('a, ~map: ('b, int) => bigint=?) => t = "BigUint64Array.from"
5051

5152
/** `fromArrayLikeOrIterableWithMap` creates a `BigUint64Array` from an array-like or iterable object and applies the mapping function to each item. The mapping function expects (value, index). See [TypedArray.from on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/from)
5253
*/
53-
@val
54+
@deprecated("Use `fromArrayLikeOrIterable` instead") @val
5455
external fromArrayLikeOrIterableWithMap: ('a, ('b, int) => bigint) => t = "BigUint64Array.from"
5556

5657
/**

0 commit comments

Comments
 (0)