Skip to content

Commit 361de87

Browse files
committed
fix variant design decisions
1 parent cdf968b commit 361de87

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

pages/docs/manual/v12.0.0/variant.mdx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -762,13 +762,16 @@ Performance-wise, a variant can potentially tremendously speed up your program's
762762

763763
```js
764764
let data = 'dog'
765-
if (data === 'dog') {
766-
...
767-
} else if (data === 'cat') {
768-
...
769-
} else if (data === 'bird') {
770-
...
765+
function logData(data) {
766+
if (data === 'dog') {
767+
...
768+
} else if (data === 'cat') {
769+
...
770+
} else if (data === 'bird') {
771+
...
772+
}
771773
}
774+
logData(data)
772775
```
773776

774777
There's a linear amount of branch checking here (`O(n)`). Compare this to using a ReScript variant:
@@ -778,16 +781,29 @@ There's a linear amount of branch checking here (`O(n)`). Compare this to using
778781
```res example
779782
type animal = Dog | Cat | Bird
780783
let data = Dog
781-
switch data {
784+
let logData = data => switch data {
782785
| Dog => Console.log("Wof")
783786
| Cat => Console.log("Meow")
784787
| Bird => Console.log("Kashiiin")
785788
}
789+
logData(data)
786790
```
787791
```js
788-
console.log("Wof");
792+
function logData(data) {
793+
switch (data) {
794+
case "Dog" :
795+
console.log("Wof");
796+
return;
797+
case "Cat" :
798+
console.log("Meow");
799+
return;
800+
case "Bird" :
801+
console.log("Kashiiin");
802+
return;
803+
}
804+
}
789805

790-
var data = "Dog";
806+
logData("Dog");
791807
```
792808

793809
</CodeTab>

0 commit comments

Comments
 (0)