on master help sign in
Add injectTyped and projectTyped. (#14)
cf4466bWill Jones
src/Data/Generics/Sum/Typed.hs+12−1
@@ -81,13 +81,24 @@ class AsType a s where
81 -- Just 2
81 -- Just 2
82 _Typed :: Prism' s a
82 _Typed :: Prism' s a
83
83
84+ -- |Inject by type.
85+ injectTyped :: a -> s
86+
87+ -- |Project by type.
88+ projectTyped :: s -> Maybe a
89+
84instance
90instance
85 ( Generic s
91 ( Generic s
86 , ErrorUnlessOne a s (CountPartialType a (Rep s))
92 , ErrorUnlessOne a s (CountPartialType a (Rep s))
87 , GAsType (Rep s) a
93 , GAsType (Rep s) a
88 ) => AsType a s where
94 ) => AsType a s where
89
95
96+ _Typed
90− _Typed = repIso . _GTyped
97+ = repIso . _GTyped
98+ injectTyped
99+ = to . ginjectTyped
100+ projectTyped
101+ = either (const Nothing) Just . gprojectTyped . from
91
102
92type family ErrorUnlessOne (a :: Type) (s :: Type) (count :: Count) :: Constraint where
103type family ErrorUnlessOne (a :: Type) (s :: Type) (count :: Count) :: Constraint where
93 ErrorUnlessOne a s 'None
104 ErrorUnlessOne a s 'None