|
1 | 1 | -- | This module defines types for effectful uncurried functions, as well as |
2 | 2 | -- | functions for converting back and forth between them. |
3 | 3 | -- | |
4 | | --- | Traditionally, it has been difficult to give a PureScript type to |
5 | | --- | JavaScript functions such as this one: |
| 4 | +-- | This makes it possible to give a PureScript type to JavaScript functions |
| 5 | +-- | such as this one: |
6 | 6 | -- | |
7 | 7 | -- | ```javascript |
8 | 8 | -- | function logMessage(level, message) { |
|
14 | 14 | -- | receiving all of its parameters, so giving it the type `Data.Function.Fn2 |
15 | 15 | -- | String String Unit`, while convenient, would effectively be a lie. |
16 | 16 | -- | |
17 | | --- | Because there has been no way of giving such functions types, we generally |
18 | | --- | resort to converting functions into the normal PureScript form (namely, |
19 | | --- | a curried function returning an Effect action), and performing the |
20 | | --- | marshalling in JavaScript, in the FFI module, like this: |
| 17 | +-- | One way to handle this would be to convert the function into the normal |
| 18 | +-- | PureScript form (namely, a curried function returning an Effect action), |
| 19 | +-- | and performing the marshalling in JavaScript, in the FFI module, like this: |
21 | 20 | -- | |
22 | 21 | -- | ```purescript |
23 | 22 | -- | -- In the PureScript file: |
|
64 | 63 | -- | |
65 | 64 | -- | (note that this has the same type as the original `logMessage`). |
66 | 65 | -- | |
67 | | --- | Effectively, we have reduced the risk of errors by moving as much code |
68 | | --- | into PureScript as possible, so that we can leverage the type system. |
69 | | --- | Hopefully, this is a little less tiresome too. |
| 66 | +-- | Effectively, we have reduced the risk of errors by moving as much code into |
| 67 | +-- | PureScript as possible, so that we can leverage the type system. Hopefully, |
| 68 | +-- | this is a little less tiresome too. |
70 | 69 | -- | |
71 | 70 | -- | Here's a slightly more advanced example. Here, because we are using |
72 | 71 | -- | callbacks, we need to use `mkEffectFn{N}` as well. |
|
121 | 120 | -- | The general naming scheme for functions and types in this module is as |
122 | 121 | -- | follows: |
123 | 122 | -- | |
124 | | --- | * `EffectFn{N}` means, a curried function which accepts N arguments and |
125 | | --- | performs some effects. The first type argument is the row of effects, |
126 | | --- | which works exactly the same way as in `Effect`. The last type argument |
127 | | --- | is the return type. All other arguments are the actual function's |
128 | | --- | arguments. |
129 | | --- | * `runEffectFn{N}` takes an `EffectFn` of N arguments, and converts it into the |
130 | | --- | normal PureScript form: a curried function which returns an Effect action. |
| 123 | +-- | * `EffectFn{N}` means, an uncurried function which accepts N arguments and |
| 124 | +-- | performs some effects. The first N arguments are the actual function's |
| 125 | +-- | argument. The last type argument is the return type. |
| 126 | +-- | * `runEffectFn{N}` takes an `EffectFn` of N arguments, and converts it into |
| 127 | +-- | the normal PureScript form: a curried function which returns an Effect |
| 128 | +-- | action. |
131 | 129 | -- | * `mkEffectFn{N}` is the inverse of `runEffectFn{N}`. It can be useful for |
132 | 130 | -- | callbacks. |
133 | 131 | -- | |
|
0 commit comments