Skip to content

Commit afe51d4

Browse files
committed
Add simple for helping type inference
1 parent bb1743b commit afe51d4

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/Data/Lens/Common.purs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,39 @@ module Data.Lens.Common
44
, module Data.Lens.Lens.Unit
55
, module Data.Lens.Prism.Either
66
, module Data.Lens.Prism.Maybe
7+
, simple
78
) where
89

10+
import Data.Lens.Types (Optic')
911
import Data.Lens.Lens.Tuple (_1, _2, first, second)
1012
import Data.Lens.Lens.Unit (united)
1113
import Data.Lens.Prism.Either (_Left, _Right, left, right)
1214
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+

src/Data/Lens/Iso/Newtype.purs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@ module Data.Lens.Iso.Newtype where
33
import Data.Lens.Iso (Iso, iso)
44
import Data.Newtype (class Newtype, wrap, unwrap)
55

6+
-- | An Iso between a newtype and its inner type.
7+
-- | Supports switching between different types that have instances of the
8+
-- | Newtype type class.
9+
-- | If you don't need to change types, you may have a better experience with
10+
-- | type inference if you use `simple _Newtype`.
611
_Newtype :: forall t a s b. Newtype t a => Newtype s b => Iso t s a b
712
_Newtype = iso unwrap wrap
13+

0 commit comments

Comments
 (0)