Skip to content

Commit cdcdf89

Browse files
authored
Add coerced (#140)
1 parent e373b45 commit cdcdf89

File tree

4 files changed

+25
-6
lines changed

4 files changed

+25
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
77
Breaking changes:
88

99
New features:
10+
- Add `coerced` (#140 by @ozkutuk)
1011
- Add `sans` and `both` (#97 by @xgrommx)
1112

1213
Bugfixes:

spago.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
, "profunctor"
2323
, "psci-support"
2424
, "record"
25+
, "safe-coerce"
2526
, "transformers"
2627
, "tuples"
2728
]

src/Data/Lens/Iso.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Data.Lens.Iso
1313
, flipped
1414
, mapping
1515
, dimapping
16+
, coerced
1617
, module Data.Lens.Types
1718
) where
1819

@@ -23,6 +24,7 @@ import Data.Maybe (Maybe(..), fromMaybe)
2324
import Data.Newtype (unwrap)
2425
import Data.Profunctor (class Profunctor, dimap, rmap)
2526
import Data.Tuple (Tuple, curry, uncurry)
27+
import Safe.Coerce (class Coercible, coerce)
2628

2729
-- | Create an `Iso` from a pair of morphisms.
2830
iso :: forall s t a b. (s -> a) -> (b -> t) -> Iso s t a b
@@ -84,3 +86,18 @@ dimapping
8486
-> AnIso ss tt aa bb
8587
-> Iso (p a ss) (q b tt) (p s aa) (q t bb)
8688
dimapping f g = withIso f \sa bt -> withIso g \ssaa bbtt -> iso (dimap sa ssaa) (dimap bt bbtt)
89+
90+
-- | An `Iso` between two types that are inferred to have the
91+
-- | same representation by the compiler. One scenario that this is
92+
-- | particularly useful is the case of nested newtypes:
93+
-- |
94+
-- |```purescript
95+
-- | newtype UserId = UserId Int
96+
-- | newtype DeletedUserId = DeletedUserId UserId
97+
-- |
98+
-- | -- `simple` is used to aid the type inference
99+
-- | deletedUser :: DeletedUserId
100+
-- | deletedUser = review (simple coerced) 42
101+
-- |```
102+
coerced :: forall s t a b. Coercible s a => Coercible t b => Iso s t a b
103+
coerced = iso coerce coerce

src/Data/Lens/Iso/Newtype.purs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module Data.Lens.Iso.Newtype where
22

3-
import Data.Lens.Iso (Iso, Iso', iso)
4-
import Data.Newtype (class Newtype, unwrap, wrap)
3+
import Data.Lens.Iso (Iso, Iso', coerced)
4+
import Data.Newtype (class Newtype)
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.
6+
-- | An Iso between a newtype and its inner type. This is a specialization of
7+
-- | `coerced` restricted to newtypes. Supports switching between different
8+
-- | types that have instances of the Newtype type class.
99
-- | If you don't need to change types, you may have a better experience with
1010
-- | type inference if you use `simple _Newtype`.
1111
_Newtype :: forall t a s b. Newtype t a => Newtype s b => Iso t s a b
12-
_Newtype = iso unwrap wrap
12+
_Newtype = coerced
1313

1414
-- | A variant of `_Newtype` which takes the constructor as an argument
1515
-- | and infers its inverse.

0 commit comments

Comments
 (0)