You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/manual/latest/polymorphic-variant.mdx
+3-4Lines changed: 3 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -412,15 +412,14 @@ In case you want to define a type that is extensible in polymorphic ways (or in
412
412
413
413
A lower bound defines the minimum set of constructors a poly variant type is aware of. It is also considered an "open poly variant type", because it doesn't restrict any additional values.
414
414
415
-
Here is an example on how to make a minimum set of `basicBlueTones` extensible for a new `colors` type:
415
+
Here is an example on how to make a minimum set of `basicBlueTones` extensible for a new `color` type:
416
416
417
417
```res
418
418
type basicBlueTone<'a> = [> #Blue | #DeepBlue | #LightBlue ] as 'a
419
419
type color = basicBlueTone<[#Blue | #DeepBlue | #LightBlue | #Purple]>
420
420
421
421
let color: color = #Purple
422
422
423
-
424
423
// This will fail due to missing minimum constructors:
425
424
type notWorking = basicBlueTone<[#Purple]>
426
425
```
@@ -447,9 +446,9 @@ type notWorking = validRed<[#Purple]>
447
446
448
447
One might think that polymorphic variants are fastly superior to common [variants](./variant). As always, it depends on the use case:
449
448
450
-
- Variants allows better encapsulation for your APIs, since they require you to define a type definition that is coupled to a specific module.
449
+
- Variants allow better encapsulation for your APIs, because they always come with a type definition that is coupled to a specific module.
451
450
- Variants are conceptionally easier to understand, makes your code easy to refactor and provides better exhaustive pattern matching support
452
451
- Variants usually deliver better type error messages, especially in recursive type definitions
453
452
- Poly variants are useful for expressing strings in JS, and allow different type composition strategies. They can also be defined adhocly in your type definitions.
454
453
455
-
In most scenarios, we'd recommend to use common variants over polymorphic variants, especially when you are writing plain ReScript code. In case you want to write zero-cost interop bindings, poly variants are a better option.
454
+
In most scenarios, we'd recommend to use common variants over polymorphic variants, especially when you are writing plain ReScript code. In case you want to write zero-cost interop bindings or generate clean JS output, poly variants are oftentimes a better option.
0 commit comments