on master help sign in
projectSub returns `Maybe sub` (fixes #21)
commitaf5e8e8d52ed44c9f4f6b13db8e86906bb013b84 authorCsongor Kiss <kiss.csongor.kiss@gmail.com> dateWed 4 Apr 2018, 21:29 (9 years ago) parent8cce226
4 files changed+15−4
ChangeLog.md+3 README.md+3 src/Data/Generics/Sum/Subtype.hs+6−4 src/Data/Generics/Sum/Typed.hs+3
ChangeLog.md+3
@@ -1,6 +1,9 @@
11## 1.0.0.0
22- TODO
33
4+### Breaking API changes
5+- `projectSub` now returns `Maybe sub` instead of `Either sup sub` (#21)
6+
47## 0.5.1.0
58- Infer input type from result type (#25)
69- Allow changing of multiple type parameters (#24)
README.md+3
@@ -33,6 +33,7 @@ Table of contents
3333 * [Prisms](#prisms)
3434 * [By name](#by-name-1)
3535 * [By type](#by-type-2)
36+* [Performance](#performance)
3637* [Contributors](#contributors)
3738
3839# Preliminaries
@@ -328,6 +329,8 @@ error:
328329Duck 6
329330```
330331
332+# Performance
333+TODO.
331334# Contributors
332335
333336+ [Matthew Pickering](https://github.com/mpickering)
src/Data/Generics/Sum/Subtype.hs+6−4
@@ -89,15 +89,17 @@ class AsSubtype sub sup where
8989 -- >>> duck ^? _Sub :: Maybe FourLeggedAnimal
9090 -- Nothing
9191 _Sub :: Prism' sup sub
92− _Sub = prism injectSub projectSub
92+ _Sub = prism injectSub (\i -> maybe (Left i) Right (projectSub i))
9393
9494 -- |Injects a subtype into a supertype (upcast).
9595 injectSub :: sub -> sup
96− injectSub = build (_Sub @sub @sup)
96+ injectSub
97+ = build (_Sub @sub @sup)
9798
9899 -- |Projects a subtype from a supertype (downcast).
99− projectSub :: sup -> Either sup sub
100− projectSub = match (_Sub @sub @sup)
100+ projectSub :: sup -> Maybe sub
101+ projectSub
102+ = either (const Nothing) Just . match (_Sub @sub @sup)
101103
102104 {-# MINIMAL (injectSub, projectSub) | _Sub #-}
103105
src/Data/Generics/Sum/Typed.hs+3
@@ -88,6 +88,7 @@ class AsType a s where
8888 -- ... Turtle
8989 -- ...
9090 _Typed :: Prism' s a
91+ _Typed = prism injectTyped (\i -> maybe (Left i) Right (projectTyped i))
9192
9293 -- |Inject by type.
9394 injectTyped :: a -> s
@@ -99,6 +100,8 @@ class AsType a s where
99100 projectTyped
100101 = either (const Nothing) Just . match _Typed
101102
103+ {-# MINIMAL (injectTyped, projectTyped) | _Typed #-}
104+
102105instance
103106 ( Generic s
104107 , ErrorUnlessOne a s (CollectPartialType as (Rep s))