diff --git a/markdown-pages/docs/manual/overview.mdx b/markdown-pages/docs/manual/overview.mdx index 62d1ed86f..7c008770b 100644 --- a/markdown-pages/docs/manual/overview.mdx +++ b/markdown-pages/docs/manual/overview.mdx @@ -57,13 +57,13 @@ order: 1 ### Number -| JavaScript | ReScript | -| ----------- | ------------ | -| `3` | Same \* | -| `3.1415` | Same | -| `3 + 4` | Same | -| `3.0 + 4.5` | `3.0 +. 4.5` | -| `5 % 3` | `mod(5, 3)` | +| JavaScript | ReScript | +| ----------- | -------- | +| `3` | Same \* | +| `3.1415` | Same | +| `3 + 4` | Same | +| `3.0 + 4.5` | Same | +| `5 % 3` | Same | \* JS has no distinction between integer and float. @@ -235,7 +235,7 @@ The last expression of a block delimited by `{}` implicitly returns (including f | ------------------------------- | ------------------------------------ | ------------------------------------------ | | String | `"Hello"` | `"Hello"` | | String Interpolation | `` `Hello ${message}` `` | `"Hello " + message` | -| Character (disrecommended) | `'x'` | `120` (char code) | +| Character (discouraged) | `'x'` | `120` (char code) | | Integer | `23`, `-23` | `23`, `-23` | | Float | `23.0`, `-23.0` | `23.0`, `-23.0` | | Integer Addition | `23 + 1` | `23 + 1` | @@ -247,7 +247,7 @@ The last expression of a block delimited by `{}` implicitly returns (including f | Comparison | `>`, `<`, `>=`, `<=` | `>`, `<`, `>=`, `<=` | | Boolean operation | `!`, `&&`, `\|\|` | `!`, `&&`, `\|\|` | | Shallow and deep Equality | `===`, `==` | `===`, `==` | -| List (disrecommended) | `list{1, 2, 3}` | `{hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}}` | +| List (discouraged) | `list{1, 2, 3}` | `{hd: 1, tl: {hd: 2, tl: {hd: 3, tl: 0}}}` | | List Prepend | `list{a1, a2, ...oldList}` | `{hd: a1, tl: {hd: a2, tl: theRest}}` | | Array | `[1, 2, 3]` | `[1, 2, 3]` | | Record | `type t = {b: int}; let a = {b: 10}` | `var a = {b: 10}` | diff --git a/markdown-pages/docs/manual/primitive-types.mdx b/markdown-pages/docs/manual/primitive-types.mdx index f5983af1a..0ce777644 100644 --- a/markdown-pages/docs/manual/primitive-types.mdx +++ b/markdown-pages/docs/manual/primitive-types.mdx @@ -148,6 +148,12 @@ ReScript's `true/false` compiles into a JavaScript `true/false`. 32-bits, truncated when necessary. We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [Int](/docs/manual/api/stdlib/int) for helper functions. +Integer operators include `+`, `-`, `*`, `/`, `**`, and `%`. + +`%` keeps the familiar `mod` naming, but its semantics are remainder (same behavior as JavaScript `%`). + +Bitwise operators for `int`: `~~~`, `&&&`, `|||`, `^^^`, `<<`, `>>`, `>>>`. + **Be careful when you bind to JavaScript numbers!** Since ReScript integers have a much smaller range than JavaScript numbers, data might get lost when dealing with large numbers. In those cases it’s much safer to bind the numbers as **float**. Be extra mindful of this when binding to JavaScript Dates and their epoch time. To improve readability, you may place underscores in the middle of numeric literals such as `1_000_000`. Note that underscores can be placed anywhere within a number, not just every three digits. @@ -156,6 +162,8 @@ To improve readability, you may place underscores in the middle of numeric liter Float requires other operators: `+.`, `-.`, `*.`, `/.`, etc. Like `0.5 +. 0.6`. See [Float](/docs/manual/api/stdlib/float) for helper functions. +Float also supports `**` (exponentiation) and `%` (remainder semantics). + As with integers, you may use underscores within literals to improve readability. ### Int-to-Float Coercion @@ -179,7 +187,7 @@ var result = 1 + 2; **Since 11.1** For values which are too large to be represented by Int or Float, there is the `bigint` primitive type. -We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [BigInt](/docs/manual/api/stdlib/bigint) for helper functions. +We provide the usual operations on them: `+`, `-`, `*`, `/`, `**`, `%`, etc. See [BigInt](/docs/manual/api/stdlib/bigint) for helper functions. A `bigint` number is denoted by a trailing `n` like so: `42n`. @@ -209,28 +217,26 @@ It also supports all the bitwise operations, except unsigned shift right (`>>>`) ```res example open! BigInt -let a = land(1n, 1n) -let b = lor(1n, 1n) -let c = lxor(1n, 1n) -let d = lnot(1n) -let e = lsl(1n, 1n) -let f = asr(1n, 1n) +let a = 5n &&& 3n +let b = 5n ||| 2n +let c = 5n ^^^ 1n +let d = ~~~5n +let e = 5n << 1n +let f = 5n >> 1n ``` ```js -var Js_bigint = require("./stdlib/js_bigint.js"); - -var a = 1n & 1n; +var a = 5n & 3n; -var b = 1n | 1n; +var b = 5n | 2n; -var c = 1n ^ 1n; +var c = 5n ^ 1n; -var d = Js_bigint.lnot(1n); +var d = ~5n; -var e = 1n << 1n; +var e = 5n << 1n; -var f = 1n >> 1n; +var f = 5n >> 1n; ``` diff --git a/markdown-pages/syntax-lookup/decorator_non_undefined.mdx b/markdown-pages/syntax-lookup/decorator_not_undefined.mdx similarity index 100% rename from markdown-pages/syntax-lookup/decorator_non_undefined.mdx rename to markdown-pages/syntax-lookup/decorator_not_undefined.mdx diff --git a/markdown-pages/syntax-lookup/language_and.mdx b/markdown-pages/syntax-lookup/language_and.mdx new file mode 100644 index 000000000..9e364e642 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_and.mdx @@ -0,0 +1,50 @@ +--- +id: "and" +keywords: ["and", "let rec and", "type rec and", "mutual recursion"] +name: "and" +summary: "This is the `and` keyword for mutually recursive definitions." +category: "languageconstructs" +--- + +Use `and` to define multiple items in one mutually-recursive group. + +### Example 1: `let rec ... and ...` + + + +```res +let rec isEven = n => n == 0 || isOdd(n - 1) +and isOdd = n => n != 0 && isEven(n - 1) +``` + +```js +function isEven(n) { + return n === 0 || isOdd((n - 1) | 0); +} + +function isOdd(n) { + return n !== 0 && isEven((n - 1) | 0); +} +``` + + + +### Example 2: `type rec ... and ...` + + + +```res +type rec expr = Add(expr, term) | Term(term) +and term = Int(int) +``` + +```js +// Type declarations are erased from JavaScript output. +``` + + + +### References + +- [Recursive Functions](../docs/manual/function.mdx#recursive-functions) +- [Type](../docs/manual/type.mdx) diff --git a/markdown-pages/syntax-lookup/language_async.mdx b/markdown-pages/syntax-lookup/language_async.mdx index 4f8a7ec2b..b3195b250 100644 --- a/markdown-pages/syntax-lookup/language_async.mdx +++ b/markdown-pages/syntax-lookup/language_async.mdx @@ -3,7 +3,7 @@ id: "async" keywords: ["async"] name: "async" summary: "This is the `async` keyword." -category: "languageconstructs" +category: "languagecontrolflow" --- **Since 10.1** diff --git a/markdown-pages/syntax-lookup/language_attached_doc_comment.mdx b/markdown-pages/syntax-lookup/language_attached_doc_comment.mdx new file mode 100644 index 000000000..78933c756 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_attached_doc_comment.mdx @@ -0,0 +1,30 @@ +--- +id: "attached-doc-comment" +keywords: ["doc comment", "attached doc comment", "/**"] +name: "/** ... */" +summary: "This is the `attached doc comment` syntax." +category: "languagecomments" +--- + +Use an attached doc comment directly before a value or type declaration. + +### Example + + + +```res +/** Returns the full name. */ +let fullName = (first, last) => first ++ " " ++ last +``` + +```js +function fullName(first, last) { + return first + " " + last; +} +``` + + + +### References + +- [Overview](../docs/manual/overview.mdx#comments) diff --git a/markdown-pages/syntax-lookup/language_await.mdx b/markdown-pages/syntax-lookup/language_await.mdx index 03240f7b1..57571364a 100644 --- a/markdown-pages/syntax-lookup/language_await.mdx +++ b/markdown-pages/syntax-lookup/language_await.mdx @@ -3,7 +3,7 @@ id: "await" keywords: ["await"] name: "await" summary: "This is the `await` keyword." -category: "languageconstructs" +category: "languagecontrolflow" --- **Since 10.1** diff --git a/markdown-pages/syntax-lookup/language_block_comment.mdx b/markdown-pages/syntax-lookup/language_block_comment.mdx new file mode 100644 index 000000000..82d038c36 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_block_comment.mdx @@ -0,0 +1,30 @@ +--- +id: "block-comment" +keywords: ["comment", "block comment", "/*", "*/"] +name: "/* ... */" +summary: "This is the `block comment` syntax." +category: "languagecomments" +--- + +Use `/* ... */` for multi-line comments. + +### Example + + + +```res +/* + This is a block comment. +*/ +let greeting = "hello" +``` + +```js +var greeting = "hello"; +``` + + + +### References + +- [Overview](../docs/manual/overview.mdx#comments) diff --git a/markdown-pages/syntax-lookup/language_char_literal.mdx b/markdown-pages/syntax-lookup/language_char_literal.mdx index 6ccb5615c..06c7a7b4c 100644 --- a/markdown-pages/syntax-lookup/language_char_literal.mdx +++ b/markdown-pages/syntax-lookup/language_char_literal.mdx @@ -3,7 +3,7 @@ id: "char-literal" keywords: ["char"] name: "' '" summary: "This is the `char` literal syntax." -category: "languageconstructs" +category: "languagepatternsvalues" --- A `char` literal is composed of two **single** quotes. Double quotes are reserved for the `string` type. Note that `char` is essentially an integer in JS since version 10.0. diff --git a/markdown-pages/syntax-lookup/language_covariant_type_parameter.mdx b/markdown-pages/syntax-lookup/language_covariant_type_parameter.mdx new file mode 100644 index 000000000..723e2f512 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_covariant_type_parameter.mdx @@ -0,0 +1,31 @@ +--- +id: "covariant-type-parameter" +keywords: ["<+a>", "+'a", "covariant", "variance", "type parameter"] +name: "<+a>" +summary: "This is a `covariant type parameter` annotation." +category: "languagetypes" +--- + +Use a `+` variance annotation on a type parameter to mark it as covariant. + +### Example + + + +```res +type source<+'a> = unit => 'a + +let fromValue = (x: 'a): source<'a> => () => x +``` + +```js +function fromValue(x) { + return (_) => x; +} +``` + + + +### References + +- [Type Parameters](../docs/manual/type.mdx#type-parameter-aka-generic) diff --git a/markdown-pages/syntax-lookup/language_dict.mdx b/markdown-pages/syntax-lookup/language_dict.mdx index dbd761de9..8209f1074 100644 --- a/markdown-pages/syntax-lookup/language_dict.mdx +++ b/markdown-pages/syntax-lookup/language_dict.mdx @@ -3,7 +3,7 @@ id: "dict" keywords: ["dict"] name: "dict" summary: "This is the `dict{}` syntax" -category: "languageconstructs" +category: "languagetypes" --- > Available in v12+ diff --git a/markdown-pages/syntax-lookup/language_doc_comment.mdx b/markdown-pages/syntax-lookup/language_doc_comment.mdx new file mode 100644 index 000000000..7738c8c06 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_doc_comment.mdx @@ -0,0 +1,32 @@ +--- +id: "doc-comment" +keywords: ["doc comment", "documentation comment", "/**", "/***"] +name: "/*** ... */" +summary: "This is the `standalone doc comment` syntax." +category: "languagecomments" +--- + +Use a standalone doc comment to write documentation text that is not attached to a specific value or type declaration. + +### Example + + + +```res +/*** This module contains utility helpers for date formatting. */ +module DateUtils = { + let formatIso = date => date +} +``` + +```js +var DateUtils = { + formatIso: (date) => date, +}; +``` + + + +### References + +- [Overview](../docs/manual/overview.mdx#comments) diff --git a/markdown-pages/syntax-lookup/language_empty_object_type.mdx b/markdown-pages/syntax-lookup/language_empty_object_type.mdx new file mode 100644 index 000000000..35943dd8a --- /dev/null +++ b/markdown-pages/syntax-lookup/language_empty_object_type.mdx @@ -0,0 +1,29 @@ +--- +id: "empty-object-type" +keywords: ["{.}", "empty object type", "object type"] +name: "{.}" +summary: "This is the `empty object type` syntax." +category: "languagetypes" +--- + +Use `{.}` to describe an object type with no known fields. + +### Example + + + +```res +type empty = {.} + +let acceptEmpty = (_value: empty) => () +``` + +```js +function acceptEmpty(_value) {} +``` + + + +### References + +- [Object](../docs/manual/object.mdx) diff --git a/markdown-pages/syntax-lookup/language_exception.mdx b/markdown-pages/syntax-lookup/language_exception.mdx new file mode 100644 index 000000000..1c586f7b6 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_exception.mdx @@ -0,0 +1,33 @@ +--- +id: "exception" +keywords: ["exception", "exn", "throw", "try", "catch"] +name: "exception" +summary: "This is the `exception` syntax." +category: "languagecontrolflow" +--- + +Use `exception` to declare custom exception constructors. + +### Example + + + +```res +exception NotFound(string) + +let message = try { + throw(NotFound("Missing value")) +} catch { +| NotFound(text) => text +} +``` + +```js +var message = "Missing value"; +``` + + + +### References + +- [Exception](../docs/manual/exception.mdx) diff --git a/markdown-pages/syntax-lookup/language_for.mdx b/markdown-pages/syntax-lookup/language_for.mdx index 7cc5f7102..e9d453863 100644 --- a/markdown-pages/syntax-lookup/language_for.mdx +++ b/markdown-pages/syntax-lookup/language_for.mdx @@ -1,9 +1,9 @@ --- id: "for" keywords: ["for", "loop"] -name: "for loop" +name: "for" summary: "This is the `for` loop." -category: "languageconstructs" +category: "languagecontrolflow" --- ReScript supports `for` loops. diff --git a/markdown-pages/syntax-lookup/language_if_else.mdx b/markdown-pages/syntax-lookup/language_if_else.mdx index 828265b7d..51b3cd2d0 100644 --- a/markdown-pages/syntax-lookup/language_if_else.mdx +++ b/markdown-pages/syntax-lookup/language_if_else.mdx @@ -3,7 +3,7 @@ id: "if-else" keywords: ["if", "else"] name: "if / else" summary: "This is the `if / else` control flow." -category: "languageconstructs" +category: "languagecontrolflow" --- Use `if / else` expressions to express a value through a `true` / `false` condition. diff --git a/markdown-pages/syntax-lookup/language_include.mdx b/markdown-pages/syntax-lookup/language_include.mdx index 8ee30a93e..f90d6bc9a 100644 --- a/markdown-pages/syntax-lookup/language_include.mdx +++ b/markdown-pages/syntax-lookup/language_include.mdx @@ -3,7 +3,7 @@ id: "include" keywords: ["include", "module"] name: "include" summary: "This is the `include` keyword." -category: "languageconstructs" +category: "languagemodules" --- The `include` keyword statically "spreads" all public values, types, modules, etc. of a given module into the current module's scope. This is sometimes useful to create supersets / mixins of different modules. diff --git a/markdown-pages/syntax-lookup/language_jsx_component.mdx b/markdown-pages/syntax-lookup/language_jsx_component.mdx new file mode 100644 index 000000000..c380667d1 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_jsx_component.mdx @@ -0,0 +1,40 @@ +--- +id: "jsx-component" +keywords: ["jsx", "", "component", "tag"] +name: "" +summary: "This is the `JSX component` syntax." +category: "languageconstructs" +--- + +Use `` to render a component in JSX. + +### Example + + + +```res +@react.component +let make = (~name) =>

{React.string("Hello " ++ name)}

+ +let view = +``` + +```js +import * as React from "react"; + +function Playground(props) { + return React.createElement("h1", undefined, "Hello " + props.name); +} + +var make = Playground; + +var view = React.createElement(Playground, { + name: "ReScript", +}); +``` + +
+ +### References + +- [Elements and JSX](../docs/react/elements-and-jsx.mdx) diff --git a/markdown-pages/syntax-lookup/language_let_rec.mdx b/markdown-pages/syntax-lookup/language_let_rec.mdx new file mode 100644 index 000000000..804d52507 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_let_rec.mdx @@ -0,0 +1,38 @@ +--- +id: "let-rec" +keywords: ["let rec", "recursive function", "recursion"] +name: "let rec" +summary: "This is the `let rec` recursive binding syntax." +category: "languageconstructs" +--- + +Use `let rec` to define recursive functions. + +### Example + + + +```res +let rec factorial = n => + if n <= 1 { + 1 + } else { + n * factorial(n - 1) + } +``` + +```js +function factorial(n) { + if (n <= 1) { + return 1; + } else { + return Math.imul(n, factorial((n - 1) | 0)); + } +} +``` + + + +### References + +- [Recursive Functions](../docs/manual/function.mdx#recursive-functions) diff --git a/markdown-pages/syntax-lookup/language_line_comment.mdx b/markdown-pages/syntax-lookup/language_line_comment.mdx new file mode 100644 index 000000000..6dca3ee3b --- /dev/null +++ b/markdown-pages/syntax-lookup/language_line_comment.mdx @@ -0,0 +1,28 @@ +--- +id: "line-comment" +keywords: ["comment", "line comment", "//"] +name: "// ... " +summary: "This is the `line comment` syntax." +category: "languagecomments" +--- + +Use `//` to write a single-line comment. + +### Example + + + +```res +// This is a line comment +let answer = 42 +``` + +```js +var answer = 42; +``` + + + +### References + +- [Overview](../docs/manual/overview.mdx#comments) diff --git a/markdown-pages/syntax-lookup/language_module.mdx b/markdown-pages/syntax-lookup/language_module.mdx index 3656d10f3..e248d81d2 100644 --- a/markdown-pages/syntax-lookup/language_module.mdx +++ b/markdown-pages/syntax-lookup/language_module.mdx @@ -3,7 +3,7 @@ id: "module" keywords: ["module"] name: "module" summary: "This is the `module` keyword." -category: "languageconstructs" +category: "languagemodules" --- `module` is used to define a scoped block of code that may contain types, `let` bindings, nested modules, etc. diff --git a/markdown-pages/syntax-lookup/language_module_function.mdx b/markdown-pages/syntax-lookup/language_module_function.mdx new file mode 100644 index 000000000..67a2f5801 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_module_function.mdx @@ -0,0 +1,58 @@ +--- +id: "module-function" +keywords: ["module function", "module", "(Item: Comparable) => {}"] +name: "module function" +summary: "A module function takes a module as input and returns a new module." +category: "languagemodules" +--- + +A `module function` lets you parameterize modules over other modules. + +### Example + + + +```res +module type Greeter = { + let greet: string => string +} + +module MakeWelcome = (G: Greeter) => { + let welcome = name => G.greet(name) ++ "!" +} + +module English = { + let greet = name => "Hello " ++ name +} + +module Welcome = MakeWelcome(English) +let message = Welcome.welcome("Ada") +``` + +```js +function MakeWelcome(G) { + function welcome(name) { + return G.greet(name) + "!"; + } + + return { + welcome: welcome, + }; +} + +var English = { + greet: function (name) { + return "Hello " + name; + }, +}; + +var Welcome = MakeWelcome(English); +var message = Welcome.welcome("Ada"); +``` + + + +### References + +- [Module Functions](../docs/manual/module-functions.mdx) +- [Module](../docs/manual/module.mdx) diff --git a/markdown-pages/syntax-lookup/language_module_type.mdx b/markdown-pages/syntax-lookup/language_module_type.mdx new file mode 100644 index 000000000..6f492b45b --- /dev/null +++ b/markdown-pages/syntax-lookup/language_module_type.mdx @@ -0,0 +1,45 @@ +--- +id: "module-type" +keywords: ["module type", "signature"] +name: "module type" +summary: "`module type` defines the required shape of a module." +category: "languagemodules" +--- + +Use `module type` to declare a module signature. + +### Example + + + +```res +module type Showable = { + type t + let show: t => string +} + +module IntShow: Showable = { + type t = int + let show = x => Int.toString(x) +} + +let rendered = IntShow.show(42) +``` + +```js +function show(x) { + return String(x); +} + +var IntShow = { + show: show, +}; + +var rendered = show(42); +``` + + + +### References + +- [Module](../docs/manual/module.mdx) diff --git a/markdown-pages/syntax-lookup/language_module_type_of.mdx b/markdown-pages/syntax-lookup/language_module_type_of.mdx new file mode 100644 index 000000000..17b62c819 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_module_type_of.mdx @@ -0,0 +1,45 @@ +--- +id: "module-type-of" +keywords: ["module type of", "module type"] +name: "module type of" +summary: "`module type of` extracts a module type from an existing module." +category: "languagemodules" +--- + +Use `module type of` when you want a signature based on an existing module. + +### Example + + + +```res +module Message = { + let greeting = "Hello" + let prefix = "[msg]" +} + +module type MessageShape = module type of Message + +module LoudMessage: MessageShape = { + let greeting = "HELLO" + let prefix = "[LOUD]" +} +``` + +```js +var Message = { + greeting: "Hello", + prefix: "[msg]", +}; + +var LoudMessage = { + greeting: "HELLO", + prefix: "[LOUD]", +}; +``` + + + +### References + +- [Module](../docs/manual/module.mdx) diff --git a/markdown-pages/syntax-lookup/language_object_type.mdx b/markdown-pages/syntax-lookup/language_object_type.mdx new file mode 100644 index 000000000..73ca77b32 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_object_type.mdx @@ -0,0 +1,32 @@ +--- +id: "object-type" +keywords: ['{"key": type}', '{"key": "value"}', "object type", "quoted fields"] +name: '{ "key": type }' +summary: "This is the `object type` syntax." +category: "languagetypes" +--- + +Use quoted field names inside `{ ... }` to define an object type. + +### Example + + + +```res +type user = {"name": string, "age": int} + +let u: user = {"name": "Ada", "age": 36} +``` + +```js +var u = { + name: "Ada", + age: 36, +}; +``` + + + +### References + +- [Object](../docs/manual/object.mdx) diff --git a/markdown-pages/syntax-lookup/language_open.mdx b/markdown-pages/syntax-lookup/language_open.mdx index 7ff16af1d..63dee0be4 100644 --- a/markdown-pages/syntax-lookup/language_open.mdx +++ b/markdown-pages/syntax-lookup/language_open.mdx @@ -3,7 +3,7 @@ id: "open" keywords: ["open", "module"] name: "open" summary: "This is the `open` keyword." -category: "languageconstructs" +category: "languagemodules" --- `open` is used to expose all values, types, modules, etc of a module in the current scope. This is useful whenever you want to use a module's functionality without typing out the module name over and over again. diff --git a/markdown-pages/syntax-lookup/language_open_object_type.mdx b/markdown-pages/syntax-lookup/language_open_object_type.mdx new file mode 100644 index 000000000..2e7509e76 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_open_object_type.mdx @@ -0,0 +1,35 @@ +--- +id: "open-object-type" +keywords: ["{..}", "open object type", "object spread type", "as 'a"] +name: "{..}" +summary: "This is the `open object type` syntax." +category: "languagetypes" +--- + +Use `{..}` to describe object types that must contain some fields but may contain additional fields. + +### Example + + + +```res +type named<'a> = {.."name": string} as 'a + +let getName: named<_> => string = obj => obj["name"] + +let name = getName({"name": "Ada", "age": 36}) +``` + +```js +function getName(obj) { + return obj.name; +} + +var name = "Ada"; +``` + + + +### References + +- [Object](../docs/manual/object.mdx) diff --git a/markdown-pages/syntax-lookup/language_optional_record_field.mdx b/markdown-pages/syntax-lookup/language_optional_record_field.mdx new file mode 100644 index 000000000..4a4096d4c --- /dev/null +++ b/markdown-pages/syntax-lookup/language_optional_record_field.mdx @@ -0,0 +1,48 @@ +--- +id: "optional-record-field" +keywords: + [ + "optional record field", + "{ ?key: type }", + "{ ?key: value }", + "name?: string", + "?", + "record", + ] +name: "{ ?key: type }" +summary: "Optional record fields can be omitted when creating records." +category: "languagetypes" +--- + +Use `?` on a record field to mark it optional. + +### Example + + + +```res +type user = { + age: int, + name?: string, +} + +let withName: user = {age: 5, name: "Ada"} +let withoutName: user = {age: 7} +``` + +```js +var withName = { + age: 5, + name: "Ada", +}; + +var withoutName = { + age: 7, +}; +``` + + + +### References + +- [Optional Record Fields](../docs/manual/record.mdx#optional-record-fields) diff --git a/markdown-pages/syntax-lookup/language_or_pattern.mdx b/markdown-pages/syntax-lookup/language_or_pattern.mdx new file mode 100644 index 000000000..9ddfb0be9 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_or_pattern.mdx @@ -0,0 +1,34 @@ +--- +id: "or-pattern" +keywords: ["|", "or pattern", "pattern matching", "switch"] +name: "| (or-pattern)" +summary: "This is the `or-pattern` syntax in `switch`." +category: "languagepatternsvalues" +--- + +Use `|` inside a pattern to match multiple cases with one branch. + +### Example + + + +```res +let n = 1 + +let label = switch n { +| 0 | 1 => "small" +| _ => "other" +} +``` + +```js +var n = 1; + +var label = n === 0 || n === 1 ? "small" : "other"; +``` + + + +### References + +- [Pattern Matching](../docs/manual/pattern-matching-destructuring.mdx) diff --git a/markdown-pages/syntax-lookup/language_polyvar.mdx b/markdown-pages/syntax-lookup/language_polyvar.mdx index 4470d02ca..9563691e0 100644 --- a/markdown-pages/syntax-lookup/language_polyvar.mdx +++ b/markdown-pages/syntax-lookup/language_polyvar.mdx @@ -3,7 +3,7 @@ id: "polyvar" keywords: ["polymorphic", "variant", "polyvar"] name: "#value" summary: "This is a `polymorphic variant`." -category: "languageconstructs" +category: "languagepatternsvalues" --- [Polymorphic variants](../docs/manual/polymorphic-variant.mdx) (or _poly variants_) are the structurally typed equivalent of [variants](../docs/manual/variant.mdx). diff --git a/markdown-pages/syntax-lookup/language_record_type.mdx b/markdown-pages/syntax-lookup/language_record_type.mdx new file mode 100644 index 000000000..c89c1b6ac --- /dev/null +++ b/markdown-pages/syntax-lookup/language_record_type.mdx @@ -0,0 +1,51 @@ +--- +id: "record-type" +keywords: + [ + "record", + "record type", + "{ key: type }", + "{ key: value }", + "myRecord", + "{name: string}", + "fields", + ] +name: "{ key: type }" +summary: "A record type is a named set of typed fields." +category: "languagetypes" +--- + +A record type defines a fixed structure with named fields. + +### Example + + + +```res +type myRecord = { + name: string, + age: int, +} + +let myRecord: myRecord = {name: "Ada", age: 36} + +let label = switch myRecord { +| {name, age} => name ++ " (" ++ Int.toString(age) ++ ")" +} +``` + +```js +var myRecord = { + name: "Ada", + age: 36, +}; + +var label = "Ada (36)"; +``` + + + +### References + +- [Record](../docs/manual/record.mdx) +- [Pattern Matching](../docs/manual/pattern-matching-destructuring.mdx) diff --git a/markdown-pages/syntax-lookup/language_ref.mdx b/markdown-pages/syntax-lookup/language_ref.mdx new file mode 100644 index 000000000..e93fe9466 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_ref.mdx @@ -0,0 +1,33 @@ +--- +id: "ref" +keywords: ["ref", ":=", "mutable", "contents"] +name: "ref" +summary: "This is the `ref` mutable reference syntax." +category: "languageconstructs" +--- + +Use `ref(value)` to create a mutable reference cell. Read via `.contents` and update via `:=`. + +### Example + + + +```res +let counter = ref(0) + +counter := counter.contents + 1 +``` + +```js +var counter = { + contents: 0, +}; + +counter.contents = (counter.contents + 1) | 0; +``` + + + +### References + +- [Mutation](../docs/manual/mutation.mdx) diff --git a/markdown-pages/syntax-lookup/language_scoped_polymorphic_type.mdx b/markdown-pages/syntax-lookup/language_scoped_polymorphic_type.mdx index bf1a695df..881be7182 100644 --- a/markdown-pages/syntax-lookup/language_scoped_polymorphic_type.mdx +++ b/markdown-pages/syntax-lookup/language_scoped_polymorphic_type.mdx @@ -3,7 +3,7 @@ id: "scoped-polymorphic-type" keywords: ["scoped", "polymorphic"] name: "'a." summary: "This is a `scoped polymorphic type`." -category: "languageconstructs" +category: "languagetypes" --- In ReScript, polymorphic functions can only deal with one specific type. When they are called, their type gets fixed. diff --git a/markdown-pages/syntax-lookup/language_string_interpolation.mdx b/markdown-pages/syntax-lookup/language_string_interpolation.mdx index 7be62d598..a381d10e4 100644 --- a/markdown-pages/syntax-lookup/language_string_interpolation.mdx +++ b/markdown-pages/syntax-lookup/language_string_interpolation.mdx @@ -3,7 +3,7 @@ id: "string-interpolation" keywords: ["string", "interpolation"] name: "``" summary: "This is the `string interpolation` syntax." -category: "languageconstructs" +category: "languagepatternsvalues" --- A `string interpolation` is composed of two backticks. It allows Unicode characters and embeddable values and expressions with the `${myValue}` syntax. diff --git a/markdown-pages/syntax-lookup/language_string_literal.mdx b/markdown-pages/syntax-lookup/language_string_literal.mdx index c2d7a412e..6ab2aa751 100644 --- a/markdown-pages/syntax-lookup/language_string_literal.mdx +++ b/markdown-pages/syntax-lookup/language_string_literal.mdx @@ -3,7 +3,7 @@ id: "string-literal" keywords: ["string"] name: '""' summary: "This is the `string` literal syntax." -category: "languageconstructs" +category: "languagepatternsvalues" --- A `string` literal is composed of two **double** quotes. Single quotes are reserved for the `char` type. diff --git a/markdown-pages/syntax-lookup/language_switch.mdx b/markdown-pages/syntax-lookup/language_switch.mdx index a07eb1794..7ddad98ef 100644 --- a/markdown-pages/syntax-lookup/language_switch.mdx +++ b/markdown-pages/syntax-lookup/language_switch.mdx @@ -3,7 +3,7 @@ id: "switch" keywords: ["switch", "pattern", "match"] name: "switch" summary: "This is the `switch` expression keyword." -category: "languageconstructs" +category: "languagecontrolflow" --- A `switch` expression allows you to match / destructure almost any kind of value (array, list, records, strings, numbers, variants, etc). Each branch (pattern) of a `switch` expression must return a value of the same type. diff --git a/markdown-pages/syntax-lookup/language_ternary.mdx b/markdown-pages/syntax-lookup/language_ternary.mdx new file mode 100644 index 000000000..41a9480b0 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_ternary.mdx @@ -0,0 +1,30 @@ +--- +id: "ternary" +keywords: ["ternary", "?:", "?", ":"] +name: "?:" +summary: "This is the `ternary` expression syntax." +category: "languagecontrolflow" +--- + +Use `condition ? whenTrue : whenFalse` as a concise conditional expression. + +### Example + + + +```res +let isAdmin = true +let greeting = isAdmin ? "Welcome, admin" : "Welcome, user" +``` + +```js +var isAdmin = true; + +var greeting = isAdmin ? "Welcome, admin" : "Welcome, user"; +``` + + + +### References + +- [If-Else & Ternary](../docs/manual/control-flow.mdx) diff --git a/markdown-pages/syntax-lookup/language_type.mdx b/markdown-pages/syntax-lookup/language_type.mdx index 3613e16fa..fd383078a 100644 --- a/markdown-pages/syntax-lookup/language_type.mdx +++ b/markdown-pages/syntax-lookup/language_type.mdx @@ -3,7 +3,7 @@ id: "type" keywords: ["type"] name: "type" summary: "This is the `type` keyword" -category: "languageconstructs" +category: "languagetypes" --- The `type` keyword is used to declare a _type_, including [Records](../docs/manual/record.mdx), [Variants](../docs/manual/variant.mdx) and [Polymorphic Variants](../docs/manual/polymorphic-variant.mdx). diff --git a/markdown-pages/syntax-lookup/language_type_parameter.mdx b/markdown-pages/syntax-lookup/language_type_parameter.mdx index f806a5f10..683aa9ea9 100644 --- a/markdown-pages/syntax-lookup/language_type_parameter.mdx +++ b/markdown-pages/syntax-lookup/language_type_parameter.mdx @@ -3,7 +3,7 @@ id: "type-parameter" keywords: ["type", "parameter", "generics", "placeholder"] name: "'a" summary: "This is a `type parameter`." -category: "languageconstructs" +category: "languagetypes" --- Types may be declared with [type parameters](../docs/manual/type.mdx#type-parameter-aka-generic) (also known as _generics_ in other languages) to create generalized versions of those types. diff --git a/markdown-pages/syntax-lookup/language_type_rec.mdx b/markdown-pages/syntax-lookup/language_type_rec.mdx new file mode 100644 index 000000000..f834b5fb0 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_type_rec.mdx @@ -0,0 +1,33 @@ +--- +id: "type-rec" +keywords: ["type rec", "recursive type", "mutually recursive types"] +name: "type rec" +summary: "`type rec` defines recursive and mutually recursive types." +category: "languagetypes" +--- + +Use `type rec` to define recursive types. Combine it with `and` for mutually recursive types. + +### Example + + + +```res +type rec expr = + | Int(int) + | Add(expr, expr) +and node = { + value: expr, + next: option, +} +``` + +```js +// Type declarations are erased from JavaScript output. +``` + + + +### References + +- [Type](../docs/manual/type.mdx) diff --git a/markdown-pages/syntax-lookup/language_variant_type.mdx b/markdown-pages/syntax-lookup/language_variant_type.mdx new file mode 100644 index 000000000..ab1470e19 --- /dev/null +++ b/markdown-pages/syntax-lookup/language_variant_type.mdx @@ -0,0 +1,52 @@ +--- +id: "variant-type" +keywords: + [ + "variant", + "variant type", + "constructors", + "A | B", + "MyVariant", + "Ok", + "Error", + ] +name: "MyVariant" +summary: "A variant type models a value as one of several named constructors." +category: "languagepatternsvalues" +--- + +A variant type lets you represent different cases in one type. + +### Example + + + +```res +type myResult<'a> = + | MyVariant('a) + | Error(string) + +let response = MyVariant(42) + +let message = switch response { +| MyVariant(value) => "Success: " ++ Int.toString(value) +| Error(err) => "Error: " ++ err +} +``` + +```js +var response = { + TAG: "MyVariant", + _0: 42, +}; + +var message = + response.TAG === "MyVariant" ? "Success: 42" : "Error: " + response._0; +``` + + + +### References + +- [Variant](../docs/manual/variant.mdx) +- [Pattern Matching](../docs/manual/pattern-matching-destructuring.mdx) diff --git a/markdown-pages/syntax-lookup/language_while.mdx b/markdown-pages/syntax-lookup/language_while.mdx index 2944238b7..2379ccde9 100644 --- a/markdown-pages/syntax-lookup/language_while.mdx +++ b/markdown-pages/syntax-lookup/language_while.mdx @@ -1,9 +1,9 @@ --- id: "while" keywords: ["while", "loop"] -name: "while loop" +name: "while" summary: "This is the `while` loop." -category: "languageconstructs" +category: "languagecontrolflow" --- ReScript supports `while` loops. While loops execute its body code block while its condition is true. diff --git a/markdown-pages/syntax-lookup/operators_bitwise_and.mdx b/markdown-pages/syntax-lookup/operators_bitwise_and.mdx new file mode 100644 index 000000000..ac80698e3 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_bitwise_and.mdx @@ -0,0 +1,27 @@ +--- +id: "bitwise-and" +keywords: ["&&&", "bitwise", "and"] +name: "&&&" +summary: "This is the `bitwise AND` operator." +category: "operators-bitwise" +--- + +`&&&` performs bitwise AND. + +### Example + + + +```res +let result = 6 &&& 3 +``` + +```js +var result = 2; +``` + + + +### References + +- [Migrate to v12](../docs/manual/migrate-to-v12.mdx) diff --git a/markdown-pages/syntax-lookup/operators_bitwise_not.mdx b/markdown-pages/syntax-lookup/operators_bitwise_not.mdx new file mode 100644 index 000000000..70e48eec7 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_bitwise_not.mdx @@ -0,0 +1,27 @@ +--- +id: "bitwise-not" +keywords: ["~~~", "bitwise", "not"] +name: "~~~" +summary: "This is the `bitwise NOT` operator." +category: "operators-bitwise" +--- + +`~~~` performs bitwise NOT. + +### Example + + + +```res +let result = ~~~2 +``` + +```js +var result = -3; +``` + + + +### References + +- [Migrate to v12](../docs/manual/migrate-to-v12.mdx) diff --git a/markdown-pages/syntax-lookup/operators_bitwise_or.mdx b/markdown-pages/syntax-lookup/operators_bitwise_or.mdx new file mode 100644 index 000000000..41fded7a6 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_bitwise_or.mdx @@ -0,0 +1,27 @@ +--- +id: "bitwise-or" +keywords: ["|||", "bitwise", "or"] +name: "|||" +summary: "This is the `bitwise OR` operator." +category: "operators-bitwise" +--- + +`|||` performs bitwise OR. + +### Example + + + +```res +let result = 6 ||| 3 +``` + +```js +var result = 7; +``` + + + +### References + +- [Migrate to v12](../docs/manual/migrate-to-v12.mdx) diff --git a/markdown-pages/syntax-lookup/operators_bitwise_xor.mdx b/markdown-pages/syntax-lookup/operators_bitwise_xor.mdx new file mode 100644 index 000000000..32d741a91 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_bitwise_xor.mdx @@ -0,0 +1,27 @@ +--- +id: "bitwise-xor" +keywords: ["^^^", "bitwise", "xor"] +name: "^^^" +summary: "This is the `bitwise XOR` operator." +category: "operators-bitwise" +--- + +`^^^` performs bitwise XOR. + +### Example + + + +```res +let result = 6 ^^^ 3 +``` + +```js +var result = 5; +``` + + + +### References + +- [Migrate to v12](../docs/manual/migrate-to-v12.mdx) diff --git a/markdown-pages/syntax-lookup/operators_boolean_and.mdx b/markdown-pages/syntax-lookup/operators_boolean_and.mdx new file mode 100644 index 000000000..f0bae17a5 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_boolean_and.mdx @@ -0,0 +1,27 @@ +--- +id: "boolean-and" +keywords: ["&&", "and", "boolean", "logical and"] +name: "&&" +summary: "This is the `boolean AND` operator." +category: "operators" +--- + +The `&&` operator returns `true` only if both sides are `true`. + +### Example + + + +```res +let canPublish = true && true +``` + +```js +var canPublish = true; +``` + + + +### References + +- [Control Flow](../docs/manual/control-flow.mdx) diff --git a/markdown-pages/syntax-lookup/operators_boolean_not.mdx b/markdown-pages/syntax-lookup/operators_boolean_not.mdx new file mode 100644 index 000000000..dae3979fe --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_boolean_not.mdx @@ -0,0 +1,27 @@ +--- +id: "boolean-not" +keywords: ["!", "not", "boolean", "logical not"] +name: "!" +summary: "This is the `boolean NOT` operator." +category: "operators" +--- + +The `!` operator flips a boolean value. + +### Example + + + +```res +let isDisabled = !false +``` + +```js +var isDisabled = true; +``` + + + +### References + +- [Control Flow](../docs/manual/control-flow.mdx) diff --git a/markdown-pages/syntax-lookup/operators_boolean_or.mdx b/markdown-pages/syntax-lookup/operators_boolean_or.mdx new file mode 100644 index 000000000..25f98e5e2 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_boolean_or.mdx @@ -0,0 +1,27 @@ +--- +id: "boolean-or" +keywords: ["||", "or", "boolean", "logical or"] +name: "||" +summary: "This is the `boolean OR` operator." +category: "operators" +--- + +The `||` operator returns `true` if at least one side is `true`. + +### Example + + + +```res +let canEdit = true || false +``` + +```js +var canEdit = true; +``` + + + +### References + +- [Control Flow](../docs/manual/control-flow.mdx) diff --git a/markdown-pages/syntax-lookup/operators_exponentiation.mdx b/markdown-pages/syntax-lookup/operators_exponentiation.mdx new file mode 100644 index 000000000..8705e0f76 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_exponentiation.mdx @@ -0,0 +1,33 @@ +--- +id: "exponentiation" +keywords: ["**", "exponentiation", "power", "int", "float", "bigint"] +name: "**" +summary: "This is the `exponentiation` operator." +category: "operators-arithmetic" +--- + +`**` raises a base value to a power. + +It supports `int`, `float`, and `bigint`. + +### Example + + + +```res +let i = 2 ** 3 +let f = 2. ** 3. + +open! BigInt +let b = 2n ** 3n +``` + +```js +var i = 8; + +var f = 8; + +var b = 2n ** 3n; +``` + + diff --git a/markdown-pages/syntax-lookup/operators_float_addition.mdx b/markdown-pages/syntax-lookup/operators_float_addition.mdx index a01fa46ec..123586d1c 100644 --- a/markdown-pages/syntax-lookup/operators_float_addition.mdx +++ b/markdown-pages/syntax-lookup/operators_float_addition.mdx @@ -3,7 +3,7 @@ id: "float-addition" keywords: ["plus", "add", "addition", "sum", "float"] name: "+." summary: "This is the `floating point addition` operator." -category: "operators" +category: "operators-arithmetic" status: "deprecated" --- diff --git a/markdown-pages/syntax-lookup/operators_float_division.mdx b/markdown-pages/syntax-lookup/operators_float_division.mdx index ae00ab720..e950b6e00 100644 --- a/markdown-pages/syntax-lookup/operators_float_division.mdx +++ b/markdown-pages/syntax-lookup/operators_float_division.mdx @@ -3,7 +3,7 @@ id: "float-division" keywords: ["divide", "division", "float"] name: "/." summary: "This is the `floating point division` operator." -category: "operators" +category: "operators-arithmetic" status: "deprecated" --- diff --git a/markdown-pages/syntax-lookup/operators_float_multiplication.mdx b/markdown-pages/syntax-lookup/operators_float_multiplication.mdx index 3457d7ae1..5e6ae81bf 100644 --- a/markdown-pages/syntax-lookup/operators_float_multiplication.mdx +++ b/markdown-pages/syntax-lookup/operators_float_multiplication.mdx @@ -3,7 +3,7 @@ id: "float-multiplication" keywords: ["multiply", "multiplication", "float"] name: "*." summary: "This is the `floating point multiplication` operator." -category: "operators" +category: "operators-arithmetic" status: "deprecated" --- diff --git a/markdown-pages/syntax-lookup/operators_float_subtraction.mdx b/markdown-pages/syntax-lookup/operators_float_subtraction.mdx index 6f71f91c9..4d2cf8019 100644 --- a/markdown-pages/syntax-lookup/operators_float_subtraction.mdx +++ b/markdown-pages/syntax-lookup/operators_float_subtraction.mdx @@ -3,7 +3,7 @@ id: "float-subtraction" keywords: ["subtract", "minus", "subtraction", "float"] name: "-." summary: "This is the `floating point subtraction` operator." -category: "operators" +category: "operators-arithmetic" status: "deprecated" --- diff --git a/markdown-pages/syntax-lookup/operators_integer_addition.mdx b/markdown-pages/syntax-lookup/operators_integer_addition.mdx index c9076dea6..874569a21 100644 --- a/markdown-pages/syntax-lookup/operators_integer_addition.mdx +++ b/markdown-pages/syntax-lookup/operators_integer_addition.mdx @@ -3,7 +3,7 @@ id: "integer-addition" keywords: ["plus", "add", "addition", "sum", "int", "integer"] name: "+" summary: "This is the `integer addition` operator." -category: "operators" +category: "operators-arithmetic" --- This operator performs _integers_ addition. diff --git a/markdown-pages/syntax-lookup/operators_integer_division.mdx b/markdown-pages/syntax-lookup/operators_integer_division.mdx index 218ee2241..d1d46b945 100644 --- a/markdown-pages/syntax-lookup/operators_integer_division.mdx +++ b/markdown-pages/syntax-lookup/operators_integer_division.mdx @@ -3,7 +3,7 @@ id: "integer-division" keywords: ["divide", "division", "int", "integer"] name: "/" summary: "This is the `integer division` operator." -category: "operators" +category: "operators-arithmetic" --- This operator performs _integer_ division, with the result truncated to an integer value. diff --git a/markdown-pages/syntax-lookup/operators_integer_multiplication.mdx b/markdown-pages/syntax-lookup/operators_integer_multiplication.mdx index da18dacb8..6694f0ac6 100644 --- a/markdown-pages/syntax-lookup/operators_integer_multiplication.mdx +++ b/markdown-pages/syntax-lookup/operators_integer_multiplication.mdx @@ -3,7 +3,7 @@ id: "integer-multiplication" keywords: ["multiply", "multiplication", "int", "integer"] name: "*" summary: "This is the `integer multiplication` operator." -category: "operators" +category: "operators-arithmetic" --- This operator performs _integer_ multiplication. diff --git a/markdown-pages/syntax-lookup/operators_integer_subtraction.mdx b/markdown-pages/syntax-lookup/operators_integer_subtraction.mdx index f7ed02581..2546521ea 100644 --- a/markdown-pages/syntax-lookup/operators_integer_subtraction.mdx +++ b/markdown-pages/syntax-lookup/operators_integer_subtraction.mdx @@ -3,7 +3,7 @@ id: "integer-subtraction" keywords: ["subtract", "minus", "subtraction", "int", "integer"] name: "-" summary: "This is the `integer subtraction` operator." -category: "operators" +category: "operators-arithmetic" --- This operator performs _integer_ subtraction. diff --git a/markdown-pages/syntax-lookup/operators_mod.mdx b/markdown-pages/syntax-lookup/operators_mod.mdx index 53b818710..f424f2690 100644 --- a/markdown-pages/syntax-lookup/operators_mod.mdx +++ b/markdown-pages/syntax-lookup/operators_mod.mdx @@ -2,11 +2,13 @@ id: "%" keywords: ["mod", "modulo", "operator", "%", "remainder"] name: "%" -summary: "This is the `modulo` operator." -category: "operators" +summary: "This is the `mod` operator (remainder semantics)." +category: "operators-arithmetic" --- -The `%` operator calculates the _modulo_ (remainder after division) of two integers. +The `%` operator has `mod` naming in ReScript, but its semantics are _remainder_ (like JavaScript `%`). + +It supports `int`, `float`, and `bigint`. ### Example diff --git a/markdown-pages/syntax-lookup/operators_shift_left.mdx b/markdown-pages/syntax-lookup/operators_shift_left.mdx new file mode 100644 index 000000000..9cf49f8a5 --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_shift_left.mdx @@ -0,0 +1,27 @@ +--- +id: "shift-left" +keywords: ["<<", "shift", "left shift", "bitwise"] +name: "<<" +summary: "This is the `left shift` operator." +category: "operators-bitwise" +--- + +`<<` shifts the bits of an integer to the left. + +### Example + + + +```res +let result = 3 << 1 +``` + +```js +var result = 6; +``` + + + +### References + +- [Primitive Types](../docs/manual/primitive-types.mdx) diff --git a/markdown-pages/syntax-lookup/operators_shift_right.mdx b/markdown-pages/syntax-lookup/operators_shift_right.mdx new file mode 100644 index 000000000..ca6a4878f --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_shift_right.mdx @@ -0,0 +1,27 @@ +--- +id: "shift-right" +keywords: [">>", "shift", "right shift", "bitwise"] +name: ">>" +summary: "This is the `right shift` operator." +category: "operators-bitwise" +--- + +`>>` shifts the bits of an integer to the right. + +### Example + + + +```res +let result = 8 >> 1 +``` + +```js +var result = 4; +``` + + + +### References + +- [Primitive Types](../docs/manual/primitive-types.mdx) diff --git a/markdown-pages/syntax-lookup/operators_shift_right_unsigned.mdx b/markdown-pages/syntax-lookup/operators_shift_right_unsigned.mdx new file mode 100644 index 000000000..5cb8a9c4d --- /dev/null +++ b/markdown-pages/syntax-lookup/operators_shift_right_unsigned.mdx @@ -0,0 +1,27 @@ +--- +id: "shift-right-unsigned" +keywords: [">>>", "shift", "unsigned right shift", "bitwise"] +name: ">>>" +summary: "This is the `unsigned right shift` operator." +category: "operators-bitwise" +--- + +`>>>` shifts the bits of an integer to the right and fills with zero bits. + +### Example + + + +```res +let result = -1 >>> 1 +``` + +```js +var result = 2147483647; +``` + + + +### References + +- [Primitive Types](../docs/manual/primitive-types.mdx) diff --git a/src/Path.res b/src/Path.res index a3bbea2fb..7f85f27ea 100644 --- a/src/Path.res +++ b/src/Path.res @@ -326,10 +326,21 @@ type t = [ | #"/syntax-lookup/specialvalues_line" | #"/syntax-lookup/specialvalues_file" | #"/syntax-lookup/operators_type_coercion" + | #"/syntax-lookup/operators_shift_right_unsigned" + | #"/syntax-lookup/operators_shift_right" + | #"/syntax-lookup/operators_shift_left" | #"/syntax-lookup/operators_triangle_pipe" | #"/syntax-lookup/operators_string_concatenation" | #"/syntax-lookup/operators_pipe" | #"/syntax-lookup/operators_mod" + | #"/syntax-lookup/operators_exponentiation" + | #"/syntax-lookup/operators_boolean_or" + | #"/syntax-lookup/operators_boolean_not" + | #"/syntax-lookup/operators_boolean_and" + | #"/syntax-lookup/operators_bitwise_xor" + | #"/syntax-lookup/operators_bitwise_or" + | #"/syntax-lookup/operators_bitwise_not" + | #"/syntax-lookup/operators_bitwise_and" | #"/syntax-lookup/operators_integer_subtraction" | #"/syntax-lookup/operators_integer_multiplication" | #"/syntax-lookup/operators_integer_division" @@ -341,30 +352,52 @@ type t = [ | #"/syntax-lookup/operator_ref_value_assignment" | #"/syntax-lookup/language_while" | #"/syntax-lookup/language_uncurried_function" + | #"/syntax-lookup/language_ref" + | #"/syntax-lookup/language_record_type" | #"/syntax-lookup/language_type_parameter" | #"/syntax-lookup/language_type" + | #"/syntax-lookup/language_type_rec" + | #"/syntax-lookup/language_ternary" | #"/syntax-lookup/language_switch" + | #"/syntax-lookup/language_or_pattern" | #"/syntax-lookup/language_string_literal" | #"/syntax-lookup/language_string_interpolation" | #"/syntax-lookup/language_spreads" | #"/syntax-lookup/language_scoped_polymorphic_type" | #"/syntax-lookup/language_regular_expression" | #"/syntax-lookup/language_polyvar" + | #"/syntax-lookup/language_open_object_type" + | #"/syntax-lookup/language_object_type" + | #"/syntax-lookup/language_optional_record_field" | #"/syntax-lookup/language_placeholder" | #"/syntax-lookup/language_optional_labeled_argument" | #"/syntax-lookup/language_open" | #"/syntax-lookup/language_module" + | #"/syntax-lookup/language_module_function" + | #"/syntax-lookup/language_module_type" + | #"/syntax-lookup/language_module_type_of" + | #"/syntax-lookup/language_jsx_component" | #"/syntax-lookup/language_let" + | #"/syntax-lookup/language_let_rec" | #"/syntax-lookup/language_labeled_argument" | #"/syntax-lookup/language_include" | #"/syntax-lookup/language_if_else" | #"/syntax-lookup/language_function" | #"/syntax-lookup/language_for" | #"/syntax-lookup/language_external" + | #"/syntax-lookup/language_exception" + | #"/syntax-lookup/language_empty_object_type" + | #"/syntax-lookup/language_doc_comment" + | #"/syntax-lookup/language_covariant_type_parameter" + | #"/syntax-lookup/language_attached_doc_comment" + | #"/syntax-lookup/language_block_comment" + | #"/syntax-lookup/language_line_comment" | #"/syntax-lookup/language_dict" | #"/syntax-lookup/language_char_literal" | #"/syntax-lookup/language_await" | #"/syntax-lookup/language_async" + | #"/syntax-lookup/language_and" + | #"/syntax-lookup/language_variant_type" | #"/syntax-lookup/extension_todo" | #"/syntax-lookup/extension_regular_expression" | #"/syntax-lookup/extension_raw_top_level_expression" @@ -393,6 +426,7 @@ type t = [ | #"/syntax-lookup/decorator_react_component" | #"/syntax-lookup/decorator_raises" | #"/syntax-lookup/decorator_obj" + | #"/syntax-lookup/decorator_not_undefined" | #"/syntax-lookup/decorator_new" | #"/syntax-lookup/decorator_module_warning" | #"/syntax-lookup/decorator_module_deprecated" diff --git a/src/SyntaxLookup.res b/src/SyntaxLookup.res index aa9a4b63c..3fd723ec0 100644 --- a/src/SyntaxLookup.res +++ b/src/SyntaxLookup.res @@ -2,6 +2,13 @@ module Category = { type t = | Decorators | Operators + | ArithmeticOperators + | BitwiseOperators + | ControlFlow + | Types + | PatternsAndValues + | Modules + | Comments | LanguageConstructs | BuiltInFunctions | ExtensionPoints @@ -12,6 +19,13 @@ module Category = { switch t { | Decorators => "Decorators" | Operators => "Operators" + | ArithmeticOperators => "Arithmetic Operators" + | BitwiseOperators => "Bitwise Operators" + | ControlFlow => "Control Flow" + | Types => "Types" + | PatternsAndValues => "Patterns & Values" + | Modules => "Modules" + | Comments => "Comments" | ExtensionPoints => "Extension Points" | BuiltInFunctions => "Built-In Functions" | LanguageConstructs => "Language Constructs" @@ -23,6 +37,13 @@ module Category = { switch s { | "decorators" => Decorators | "operators" => Operators + | "operators-arithmetic" => ArithmeticOperators + | "operators-bitwise" => BitwiseOperators + | "languagecontrolflow" => ControlFlow + | "languagetypes" => Types + | "languagepatternsvalues" => PatternsAndValues + | "languagemodules" => Modules + | "languagecomments" => Comments | "languageconstructs" => LanguageConstructs | "builtinfunctions" => BuiltInFunctions | "extensionpoints" => ExtensionPoints @@ -74,9 +95,26 @@ module Item = { href: string, } + let quoteLiteralRank = name => + switch name { + | "\"\"" => 0 + | "``" => 1 + | "' '" => 2 + | _ => 3 + } + + let compareNames = (a, b) => { + let rankA = quoteLiteralRank(a) + let rankB = quoteLiteralRank(b) + switch Int.compare(rankA, rankB) { + | 0. => String.compare(a, b) + | x => x + } + } + let compare = (a, b) => switch Status.compare(a.status, b.status) { - | 0. => String.compare(a.name, b.name) + | 0. => compareNames(a.name, b.name) | x => x } } @@ -278,12 +316,19 @@ let make = ( } /* - Order all items in tag groups + Group all items into top-level sections with optional subsections. */ let categories = { let initial = [ Category.Decorators, Operators, + ArithmeticOperators, + BitwiseOperators, + ControlFlow, + Types, + PatternsAndValues, + Modules, + Comments, LanguageConstructs, BuiltInFunctions, ExtensionPoints, @@ -299,7 +344,7 @@ let make = ( | ShowFiltered(_, items) => items } - Array.reduce(items, Dict.fromArray(initial), (acc, item) => { + let grouped = Array.reduce(items, Dict.fromArray(initial), (acc, item) => { let key = item.category->Category.toString Dict.get(acc, key)->Option.mapOr(acc, items => { Array.push(items, item)->ignore @@ -307,32 +352,84 @@ let make = ( acc }) }) - ->Dict.toArray - ->Array.reduce([], (acc, entry) => { - let (title, items) = entry + + let getItems = category => Dict.get(grouped, category->Category.toString)->Option.getOr([]) + + let renderTags = items => + items + ->Array.toSorted(Item.compare) + ->Array.map(item => { + let onMouseDown = evt => { + ReactEvent.Mouse.preventDefault(evt) + onSearchValueChange(item.name) + } + + + + }) + ->React.array + + let renderSubsection = (~showHeading, title, items) => if Array.length(items) === 0 { - acc + None } else { - let children = - items - ->Array.toSorted(Item.compare) - ->Array.map(item => { - let onMouseDown = evt => { - ReactEvent.Mouse.preventDefault(evt) - onSearchValueChange(item.name) - } - - - - }) - let el = -
- {React.array(children)} -
- Array.push(acc, el)->ignore - acc +
+ {if showHeading { +

+ {React.string(title)} +

+ } else { + React.null + }} +
{renderTags(items)}
+
->Some } - }) + + let renderSection = (title, subsections) => { + let subsections = + subsections->Array.filterMap(((subtitle, items)) => + renderSubsection(~showHeading=subtitle !== title, subtitle, items) + ) + if Array.length(subsections) === 0 { + None + } else { +
+

+ {React.string(title)} +

+
{React.array(subsections)}
+
->Some + } + } + + let sections = [ + ("Decorators", [("Decorators", getItems(Decorators))]), + ( + "Operators", + [ + ("Operators", getItems(Operators)), + ("Arithmetic", getItems(ArithmeticOperators)), + ("Bitwise", getItems(BitwiseOperators)), + ], + ), + ( + "Language Constructs", + [ + ("Language Constructs", getItems(LanguageConstructs)), + ("Control Flow", getItems(ControlFlow)), + ("Types", getItems(Types)), + ("Patterns & Values", getItems(PatternsAndValues)), + ("Modules", getItems(Modules)), + ("Comments", getItems(Comments)), + ], + ), + ("Built-In Functions", [("Built-In Functions", getItems(BuiltInFunctions))]), + ("Extension Points", [("Extension Points", getItems(ExtensionPoints))]), + ("Special Values", [("Special Values", getItems(SpecialValues))]), + ("Other", [("Other", getItems(Other))]), + ] + + sections->Array.filterMap(((title, subsections)) => renderSection(title, subsections)) } let (searchValue, completionItems) = React.useMemo(() =>