Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix variant design decisions #1024

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

Merged
merged 2 commits into from
May 5, 2025
Merged
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
43 changes: 31 additions & 12 deletions pages/docs/manual/v12.0.0/variant.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,16 @@ Performance-wise, a variant can potentially tremendously speed up your program's

```js
let data = 'dog'
if (data === 'dog') {
...
} else if (data === 'cat') {
...
} else if (data === 'bird') {
...
function logData(data) {
if (data === 'dog') {
...
} else if (data === 'cat') {
...
} else if (data === 'bird') {
...
}
}
logData(data)
```

There's a linear amount of branch checking here (`O(n)`). Compare this to using a ReScript variant:
Expand All @@ -778,16 +781,32 @@ There's a linear amount of branch checking here (`O(n)`). Compare this to using
```res example
type animal = Dog | Cat | Bird
let data = Dog
switch data {
| Dog => Console.log("Wof")
| Cat => Console.log("Meow")
| Bird => Console.log("Kashiiin")
let logData = data => {
switch data {
| Dog => Console.log("Wof")
| Cat => Console.log("Meow")
| Bird => Console.log("Kashiiin")
}
}
logData(data)

```
```js
console.log("Wof");
function logData(data) {
switch (data) {
case "Dog" :
console.log("Wof");
return;
case "Cat" :
console.log("Meow");
return;
case "Bird" :
console.log("Kashiiin");
return;
}
}

var data = "Dog";
logData("Dog");
```

</CodeTab>
Expand Down