Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax Lookup: More coercion docs #763

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions compilers/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compilers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"rescript-912": "npm:[email protected]",
"rescript-1000": "npm:[email protected]",
"rescript-1010": "npm:[email protected]",
"rescript-1100": "npm:[email protected].4"
"rescript-1100": "npm:[email protected].8"
}
}
64 changes: 61 additions & 3 deletions misc_docs/syntax/operators_type_coercion.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ category: "operators"

The `:>` operator may be used to convert a polymorphic variant to a `string` or `int`, or convert an [object](/docs/manual/latest/object) to a type with a subset of its fields.

_Since ReScript `11.0.0`_ coercion also works for converting
- from `int` to `float`
- from record to record with the same field(s) or when record A is a subtype of record B
- from `@unboxed` variant with only strings to `string`
- from `string` to `@unboxed` variant that have a catch-all unboxed `string` case
- from variant to variant when applicable
- from variant to `string`/`int`/`float` when applicable
- for invariant type arguments such as array payloads when the runtime representation is guaranteed to be exactly the same

### Example 1

<CodeTab labels={["ReScript", "JS Output"]}>

```res
```res example
type color = [#Red | #Green | #Blue]
let color: color = #Red
let message = "The color is " ++ (color :> string)
Expand All @@ -28,7 +37,7 @@ var message = "The color is Red";

<CodeTab labels={["ReScript", "JS Output"]}>

```res
```res example
type bit = [#0 | #1]
let bit: bit = #1
let value = (bit :> int)
Expand All @@ -45,7 +54,7 @@ var value = 1;

<CodeTab labels={["ReScript", "JS Output"]}>

```res
```res example
type person = {"id": int, "name": string}
type name = {"name": string}
let person = {"id": 10, "name": "Gideon"}
Expand All @@ -63,7 +72,56 @@ var name = person;

</CodeTab>

### Example 4

<CodeTab labels={["ReScript", "JS Output"]}>

```res example
@unboxed
type myNumberType = One | Two | Other(string)

let v = Other("Infinite")
let v2 = One

let x = "NaN"

// variant to string
Console.log((v :> string))
Console.log((v2 :> string))

// string to variant
let y = (x :> myNumberType)

let f = switch y {
| One => "One"
| _ => "Two"
}
```

```js
var v = "Infinite";

var x = "NaN";

console.log(v);

console.log("One");

var f;

f = (x === "One" || x === "Two") && x === "One" ? "One" : "Two";

var v2 = "One";

var y = x;
```

</CodeTab>


### References

* [Int-to-Float Coercion](/docs/manual/latest/primitive-types#int-to-float-coercion)
* [Variant Coercion](/docs/manual/latest/variant#coercion)
* [Polymorphic Variant Coercion](/docs/manual/latest/polymorphic-variant#coercion)
* [Record Type Coercion](/docs/manual/latest/record#record-type-coercion)
15 changes: 15 additions & 0 deletions pages/docs/manual/latest/primitive-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ Float requires other operators: `+.`, `-.`, `*.`, `/.`, etc. Like `0.5 +. 0.6`.

As with integers, you may use underscores within literals to improve readability.

### Int-to-Float Coercion

`int` values can be coerced to `float` with the `:>` (type coercion) operator.

<CodeTab labels={["ReScript", "JS Output"]}>

```res example
let result = (1 :> float) +. 2.
```
```js
var result = 1 + 2;
```

</CodeTab>

## Unit

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?
Expand Down
Loading