Skip to content
This repository was archived by the owner on Nov 26, 2020. It is now read-only.

Commit 80622be

Browse files
committed
Add scoped type variable example
1 parent 5572418 commit 80622be

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,27 @@ class AjaxResponse a where
3030

3131
We can now call `responseType (Proxy :: Proxy SomeContentType)` to produce
3232
a `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

src/Type/Proxy.purs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,27 @@
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+
-- | ```
2949
module Type.Proxy where
3050

3151
-- | Value proxy for kind `*` types.

0 commit comments

Comments
 (0)