Skip to content

Commit a63059e

Browse files
authored
Fix variant design decisions (#1024)
* fix variant design decisions * format
1 parent 7fe1574 commit a63059e

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

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

Lines changed: 31 additions & 12 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,32 @@ 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 {
782-
| Dog => Console.log("Wof")
783-
| Cat => Console.log("Meow")
784-
| Bird => Console.log("Kashiiin")
784+
let logData = data => {
785+
switch data {
786+
| Dog => Console.log("Wof")
787+
| Cat => Console.log("Meow")
788+
| Bird => Console.log("Kashiiin")
789+
}
785790
}
791+
logData(data)
792+
786793
```
787794
```js
788-
console.log("Wof");
795+
function logData(data) {
796+
switch (data) {
797+
case "Dog" :
798+
console.log("Wof");
799+
return;
800+
case "Cat" :
801+
console.log("Meow");
802+
return;
803+
case "Bird" :
804+
console.log("Kashiiin");
805+
return;
806+
}
807+
}
789808

790-
var data = "Dog";
809+
logData("Dog");
791810
```
792811

793812
</CodeTab>

0 commit comments

Comments
 (0)