|
| 1 | +-- | The `Proxy` type and values are for situations where type information is |
| 2 | +-- | required for an input to determine the type of an output, but where it is |
| 3 | +-- | not possible or convenient to provide a _value_ for the input. |
| 4 | +-- | |
| 5 | +-- | A hypothetical example: if you have a class that is used to handle the |
| 6 | +-- | result of an AJAX request, you may want to use this information to set the |
| 7 | +-- | expected content type of the request, so you might have a class something |
| 8 | +-- | like this: |
| 9 | +-- | |
| 10 | +-- | ``` purescript |
| 11 | +-- | class AjaxResponse a where |
| 12 | +-- | responseType :: a -> ResponseType |
| 13 | +-- | fromResponse :: Foreign -> a |
| 14 | +-- | ``` |
| 15 | +-- | |
| 16 | +-- | The problem here is `responseType` requires a value of type `a`, but we |
| 17 | +-- | won't have a value of that type until the request has been completed. The |
| 18 | +-- | solution is to use a `Proxy` type instead: |
| 19 | +-- | |
| 20 | +-- | ``` purescript |
| 21 | +-- | class AjaxResponse a where |
| 22 | +-- | responseType :: Proxy a -> ResponseType |
| 23 | +-- | fromResponse :: Foreign -> a |
| 24 | +-- | ``` |
| 25 | +-- | |
| 26 | +-- | We can now call `responseType (Proxy :: Proxy SomeContentType)` to produce |
| 27 | +-- | a `ResponseType` for `SomeContentType` without having to construct some |
| 28 | +-- | empty version of `SomeContentType` first. |
| 29 | +module Type.Proxy where |
| 30 | + |
| 31 | +-- | Value proxy for kind `*` types. |
| 32 | +data Proxy a = Proxy |
| 33 | + |
| 34 | +-- | Value proxy for kind `* -> *` types. |
| 35 | +data Proxy2 (a :: * -> *) = Proxy2 |
| 36 | + |
| 37 | +-- | Value proxy for kind `* -> * -> *` types. |
| 38 | +data Proxy3 (a :: * -> * -> *) = Proxy3 |
0 commit comments