Skip to content

Commit 9c7c9e6

Browse files
Merge pull request #835 from rescript-association/bigint
BigInt docs
2 parents f117994 + af1e019 commit 9c7c9e6

File tree

4 files changed

+98
-8
lines changed

4 files changed

+98
-8
lines changed

compilers/package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compilers/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"rescript-1000": "npm:rescript@10.0.0",
1111
"rescript-1010": "npm:rescript@10.1.0",
1212
"rescript-1100": "npm:rescript@11.0.0",
13-
"rescript-1110": "npm:rescript@11.1.0-rc.2",
13+
"rescript-1110": "npm:rescript@11.1.0-rc.6",
1414
"rescript-820": "npm:bs-platform@8.2.0",
1515
"rescript-902": "npm:bs-platform@9.0.2",
1616
"rescript-912": "npm:rescript@9.1.2"

pages/docs/manual/latest/primitive-types.mdx

+89
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,95 @@ var result = 1 + 2;
166166

167167
</CodeTab>
168168

169+
## Big Integers (experimental)
170+
171+
**Since 11.1**
172+
173+
For values which are too large to be represented by Int or Float, there is the `bigint` primitive type.
174+
We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [BigInt](api/core/bigint) for helper functions.
175+
176+
A `bigint` number is denoted by a trailing `n` like so: `42n`.
177+
178+
As `bigint` is a different data type than `int`, it's necessary to open the corresponding module to overload the operators.
179+
180+
<CodeTab labels={["ReScript", "JS Output"]}>
181+
182+
```res example
183+
open! Js.BigInt
184+
185+
let a = 9007199254740991n + 9007199254740991n
186+
let b = 2n ** 2n
187+
```
188+
```js
189+
var a = 9007199254740991n + 9007199254740991n;
190+
191+
var p = 2n ** 2n;
192+
```
193+
194+
</CodeTab>
195+
196+
It also supports all the bitwise operations, except unsigned shift right (`>>>`), which is not supported by JS itself for `bigint`s.
197+
198+
<CodeTab labels={["ReScript", "JS Output"]}>
199+
200+
```res example
201+
open! Js.BigInt
202+
203+
let a = land(1n, 1n)
204+
let b = lor(1n, 1n)
205+
let c = lxor(1n, 1n)
206+
let d = lnot(1n)
207+
let e = lsl(1n, 1n)
208+
let f = asr(1n, 1n)
209+
```
210+
```js
211+
var Js_bigint = require("./stdlib/js_bigint.js");
212+
213+
var a = 1n & 1n;
214+
215+
var b = 1n | 1n;
216+
217+
var c = 1n ^ 1n;
218+
219+
var d = Js_bigint.lnot(1n);
220+
221+
var e = (1n << 1n);
222+
223+
var f = (1n >> 1n);
224+
```
225+
226+
</CodeTab>
227+
228+
It can also be pattern-matched.
229+
230+
<CodeTab labels={["ReScript", "JS Output"]}>
231+
232+
```res example
233+
let bigintValue = 1n
234+
235+
switch bigintValue {
236+
| 1n => Console.log("Small bigint")
237+
| 100n => Console.log("Larger bigint")
238+
| _ => Console.log("Other bigint")
239+
}
240+
```
241+
```js
242+
if (1n !== 1n) {
243+
if (1n !== 100n) {
244+
console.log("Other bigint");
245+
} else {
246+
console.log("Larger bigint");
247+
}
248+
} else {
249+
console.log("Small bigint");
250+
}
251+
252+
var bigintValue = 1n;
253+
```
254+
255+
</CodeTab>
256+
257+
169258
## Unit
170259

171260
The `unit` type indicates the absence of a specific value. It has only a single value, `()`, which acts as a placeholder when no other value exists or is needed. It compiles to JavaScript's `undefined` and resembles the `void` type in languages such as C++. What's the point of such a type?

pages/docs/manual/latest/variant.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ Here's a list of all possible things you can unbox:
365365
- `string`: `String(string)`
366366
- `float`: `Float(float)`. Note you can only have one of `float` or `int` because JavaScript only has `number` (not actually `int` and `float` like in ReScript) so we can't disambiguate between `float` and `int` at runtime.
367367
- `int`: `Int(int)`. See note above on `float`.
368+
- `bigint`: `BigInt(int)`. **Since 11.1** This is a distinct type from JavaScript's `number` type so you can use it beside either `float` or `int`.
368369
- `bool`: `Boolean(bool)`
369370
- `array<'value>`: `List(array<string>)`
370371
- `('a, 'b, 'c)`: `Tuple((string, int, bool))`. Any size of tuples works, but you can have only one case of array or tuple in a variant.

0 commit comments

Comments
 (0)