This repository was archived by the owner on Nov 26, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Expand file tree Collapse file tree 2 files changed +42
-2
lines changed Original file line number Diff line number Diff line change @@ -30,7 +30,27 @@ class AjaxResponse a where
3030
3131We can now call ` responseType (Proxy :: Proxy SomeContentType) ` to produce
3232a ` ResponseType ` for ` SomeContentType ` without having to construct some
33- empty version of ` SomeContentType ` first.
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+ ```
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))
53+ ```
3454
3555#### ` Proxy `
3656
Original file line number Diff line number Diff line change 2525-- |
2626-- | We can now call `responseType (Proxy :: Proxy SomeContentType)` to produce
2727-- | a `ResponseType` for `SomeContentType` without having to construct some
28- -- | empty version of `SomeContentType` first.
28+ -- | empty version of `SomeContentType` first. In situations like this where
29+ -- | the `Proxy` type can be statically determined, it is recommended to pull
30+ -- | out the definition to the top level and make a declaration like:
31+ -- |
32+ -- | ``` purescript
33+ -- | _SomeContentType :: Proxy SomeContentType
34+ -- | _SomeContentType = Proxy
35+ -- | ```
36+ -- |
37+ -- | That way the proxy value can be used as `responseType _SomeContentType`
38+ -- | for improved readability. However, this is not always possible, sometimes
39+ -- | the type required will be determined by a type variable. As PureScript has
40+ -- | scoped type variables, we can do things like this:
41+ -- |
42+ -- | ``` purescript
43+ -- | makeRequest :: URL -> ResponseType -> Aff _ Foreign
44+ -- | makeRequest = ...
45+ -- |
46+ -- | fetchData :: forall a. (AjaxResponse a) => URL -> Aff _ a
47+ -- | fetchData url = fromResponse <$> makeRequest url (responseType (Proxy :: Proxy a))
48+ -- | ```
2949module Type.Proxy where
3050
3151-- | Value proxy for kind `*` types.
You can’t perform that action at this time.
0 commit comments