@@ -6,24 +6,10 @@ summary: "This is the `@unwrap` decorator."
6
6
category : " decorators"
7
7
---
8
8
9
- The ` @unwrap ` decorator may be used when binding to external functions that accept multiple types for an argument.
9
+ The ` @unwrap ` decorator may be used when binding to external functions
10
+ that accept multiple types for an argument.
10
11
11
- Consider the following JavaScript function:
12
-
13
- ``` js
14
- function padLeft (padding , str ) {
15
- if (typeof padding === " number" ) {
16
- return " " .repeat (padding) + str;
17
- }
18
- if (typeof padding === " string" ) {
19
- return padding + str;
20
- }
21
- throw new Error (" Expected padding to be number or string" );
22
- }
23
- ```
24
- Note how ` padding ` can be either a number or a string.
25
-
26
- Here is how you'd bind to this function:
12
+ ### Example
27
13
28
14
<CodeTab labels = { [" ReScript" , " JS Output" ]} >
29
15
@@ -33,16 +19,17 @@ Here is how you'd bind to this function:
33
19
string
34
20
) => string = "padLeft";
35
21
36
- let _ = padLeft(#Int(7), "eleven");
37
- let _ = padLeft(#Str("7"), "eleven");
22
+ let result1 = padLeft(#Int(7), "eleven");
23
+ let result2 = padLeft(#Str("7"), "eleven");
38
24
```
39
25
40
26
``` js
41
- padLeft (7 , " eleven" );
42
-
43
- padLeft (" 7" , " eleven" );
27
+ var result1 = padLeft (7 , " eleven" );
28
+ var result2 = padLeft (" 7" , " eleven" );
44
29
```
45
30
46
31
</CodeTab >
47
32
48
- Review the JavaScript output and note how ` @unwrap ` simply unwraps the polymorphic variant constructor.
33
+ ### References
34
+
35
+ * [ Modeling Polymorphic Function] ( /docs/manual/latest/bind-to-js-function#modeling-polymorphic-function )
0 commit comments