@@ -4,9 +4,39 @@ module Data.Lens.Common
4
4
, module Data.Lens.Lens.Unit
5
5
, module Data.Lens.Prism.Either
6
6
, module Data.Lens.Prism.Maybe
7
+ , simple
7
8
) where
8
9
10
+ import Data.Lens.Types (Optic' )
9
11
import Data.Lens.Lens.Tuple (_1 , _2 , first , second )
10
12
import Data.Lens.Lens.Unit (united )
11
13
import Data.Lens.Prism.Either (_Left , _Right , left , right )
12
14
import Data.Lens.Prism.Maybe (_Just , _Nothing )
15
+
16
+ -- | This is useful for when you want to restrict the type of another optic.
17
+ -- | For example, suppose you have the following declarations:
18
+ -- | ```purescript
19
+ -- | newtype X = X Int
20
+ -- | derive instance newtypeX :: Newtype X _
21
+ -- | ```
22
+ -- |
23
+ -- | Attempting to view with the `_Newtype` optic:
24
+ -- | ```purescript
25
+ -- | X 42 ^. _Newtype
26
+ -- | ```
27
+ -- | Will result in a type error:
28
+ -- | ```
29
+ -- | The inferred type
30
+ -- | forall t3 t5. Newtype t3 t5 => Int
31
+ -- | has type variables which are not mentioned in the body of the type.
32
+ -- | Consider adding a type annotation.
33
+ -- | ```
34
+ -- |
35
+ -- | However, if we apply the `simple` function:
36
+ -- | ```purescript
37
+ -- | X 42 ^. simple _Newtype
38
+ -- | ```
39
+ -- | We get the expected result `42`.
40
+ simple :: forall p s a . Optic' p s a -> Optic' p s a
41
+ simple x = x
42
+
0 commit comments