on master help sign in
projectSub returns `Maybe sub` (fixes #21)
af5e8e8Csongor Kiss
ChangeLog.md+3
@@ -1,6 +1,9 @@
1## 1.0.0.0
1## 1.0.0.0
2- TODO
2- TODO
3
3
4+### Breaking API changes
5+- `projectSub` now returns `Maybe sub` instead of `Either sup sub` (#21)
6+
4## 0.5.1.0
7## 0.5.1.0
5- Infer input type from result type (#25)
8- Infer input type from result type (#25)
6- Allow changing of multiple type parameters (#24)
9- Allow changing of multiple type parameters (#24)
README.md+3
@@ -33,6 +33,7 @@ Table of contents
33 * [Prisms](#prisms)
33 * [Prisms](#prisms)
34 * [By name](#by-name-1)
34 * [By name](#by-name-1)
35 * [By type](#by-type-2)
35 * [By type](#by-type-2)
36+* [Performance](#performance)
36* [Contributors](#contributors)
37* [Contributors](#contributors)
37
38
38# Preliminaries
39# Preliminaries
@@ -328,6 +329,8 @@ error:
328Duck 6
329Duck 6
329```
330```
330
331
332+# Performance
333+TODO.
331# Contributors
334# Contributors
332
335
333+ [Matthew Pickering](https://github.com/mpickering)
336+ [Matthew Pickering](https://github.com/mpickering)
src/Data/Generics/Sum/Subtype.hs+6−4
@@ -89,15 +89,17 @@ class AsSubtype sub sup where
89 -- >>> duck ^? _Sub :: Maybe FourLeggedAnimal
89 -- >>> duck ^? _Sub :: Maybe FourLeggedAnimal
90 -- Nothing
90 -- Nothing
91 _Sub :: Prism' sup sub
91 _Sub :: Prism' sup sub
92− _Sub = prism injectSub projectSub
92+ _Sub = prism injectSub (\i -> maybe (Left i) Right (projectSub i))
93
93
94 -- |Injects a subtype into a supertype (upcast).
94 -- |Injects a subtype into a supertype (upcast).
95 injectSub :: sub -> sup
95 injectSub :: sub -> sup
96+ injectSub
96− injectSub = build (_Sub @sub @sup)
97+ = build (_Sub @sub @sup)
97
98
98 -- |Projects a subtype from a supertype (downcast).
99 -- |Projects a subtype from a supertype (downcast).
99− projectSub :: sup -> Either sup sub
100+ projectSub :: sup -> Maybe sub
100− projectSub = match (_Sub @sub @sup)
101+ projectSub
102+ = either (const Nothing) Just . match (_Sub @sub @sup)
101
103
102 {-# MINIMAL (injectSub, projectSub) | _Sub #-}
104 {-# MINIMAL (injectSub, projectSub) | _Sub #-}
103
105
src/Data/Generics/Sum/Typed.hs+3
@@ -88,6 +88,7 @@ class AsType a s where
88 -- ... Turtle
88 -- ... Turtle
89 -- ...
89 -- ...
90 _Typed :: Prism' s a
90 _Typed :: Prism' s a
91+ _Typed = prism injectTyped (\i -> maybe (Left i) Right (projectTyped i))
91
92
92 -- |Inject by type.
93 -- |Inject by type.
93 injectTyped :: a -> s
94 injectTyped :: a -> s
@@ -99,6 +100,8 @@ class AsType a s where
99 projectTyped
100 projectTyped
100 = either (const Nothing) Just . match _Typed
101 = either (const Nothing) Just . match _Typed
101
102
103+ {-# MINIMAL (injectTyped, projectTyped) | _Typed #-}
104+
102instance
105instance
103 ( Generic s
106 ( Generic s
104 , ErrorUnlessOne a s (CollectPartialType as (Rep s))
107 , ErrorUnlessOne a s (CollectPartialType as (Rep s))