|
1 | | -# Module Documentation |
| 1 | +# purescript-proxy |
2 | 2 |
|
3 | | -## Module Type.Proxy |
| 3 | +[](https://github.com/purescript/purescript-proxy/releases) |
| 4 | +[](https://travis-ci.org/purescript/purescript-proxy) |
4 | 5 |
|
| 6 | +Value proxy for type inputs. |
5 | 7 |
|
6 | | -The `Proxy` type and values are for situations where type information is |
7 | | -required for an input to determine the type of an output, but where it is |
8 | | -not possible or convenient to provide a _value_ for the input. |
| 8 | +## Installation |
9 | 9 |
|
10 | | -A hypothetical example: if you have a class that is used to handle the |
11 | | -result of an AJAX request, you may want to use this information to set the |
12 | | -expected content type of the request, so you might have a class something |
13 | | -like this: |
14 | | - |
15 | | -``` purescript |
16 | | -class AjaxResponse a where |
17 | | - responseType :: a -> ResponseType |
18 | | - fromResponse :: Foreign -> a |
19 | | -``` |
20 | | - |
21 | | -The problem here is `responseType` requires a value of type `a`, but we |
22 | | -won't have a value of that type until the request has been completed. The |
23 | | -solution is to use a `Proxy` type instead: |
24 | | - |
25 | | -``` purescript |
26 | | -class AjaxResponse a where |
27 | | - responseType :: Proxy a -> ResponseType |
28 | | - fromResponse :: Foreign -> a |
29 | | -``` |
30 | | - |
31 | | -We can now call `responseType (Proxy :: Proxy SomeContentType)` to produce |
32 | | -a `ResponseType` for `SomeContentType` without having to construct some |
33 | | -empty version of `SomeContentType` first. In situations like this where |
34 | | -the `Proxy` type can be statically determined, it is recommended to pull |
35 | | -out the definition to the top level and make a declaration like: |
36 | | - |
37 | | -``` purescript |
38 | | -_SomeContentType :: Proxy SomeContentType |
39 | | -_SomeContentType = Proxy |
40 | 10 | ``` |
41 | | - |
42 | | -That way the proxy value can be used as `responseType _SomeContentType` |
43 | | -for improved readability. However, this is not always possible, sometimes |
44 | | -the type required will be determined by a type variable. As PureScript has |
45 | | -scoped type variables, we can do things like this: |
46 | | - |
47 | | -``` purescript |
48 | | -makeRequest :: URL -> ResponseType -> Aff _ Foreign |
49 | | -makeRequest = ... |
50 | | -
|
51 | | -fetchData :: forall a. (AjaxResponse a) => URL -> Aff _ a |
52 | | -fetchData url = fromResponse <$> makeRequest url (responseType (Proxy :: Proxy a)) |
| 11 | +bower install purescript-proxy |
53 | 12 | ``` |
54 | 13 |
|
55 | | -#### `Proxy` |
56 | | - |
57 | | -``` purescript |
58 | | -data Proxy a |
59 | | - = Proxy |
60 | | -``` |
61 | | - |
62 | | -Value proxy for kind `*` types. |
63 | | - |
64 | | -#### `Proxy2` |
65 | | - |
66 | | -``` purescript |
67 | | -data Proxy2 (a :: * -> *) |
68 | | - = Proxy2 |
69 | | -``` |
70 | | - |
71 | | -Value proxy for kind `* -> *` types. |
72 | | - |
73 | | -#### `Proxy3` |
74 | | - |
75 | | -``` purescript |
76 | | -data Proxy3 (a :: * -> * -> *) |
77 | | - = Proxy3 |
78 | | -``` |
79 | | - |
80 | | -Value proxy for kind `* -> * -> *` types. |
81 | | - |
82 | | - |
| 14 | +## Module documentation |
83 | 15 |
|
| 16 | +- [Type.Proxy](docs/Type/Proxy.md) |
0 commit comments