on master help sign in
Add injectTyped and projectTyped. (#14)
commitcf4466b3de5fbcb653dd5be6bfc6187b9de437bb authorWill Jones <will@sacharissa.co.uk> dateWed 6 Sep 2017, 16:18 (9 years ago) parentee5e333
1 file changed+12−1
src/Data/Generics/Sum/Typed.hs+12−1
src/Data/Generics/Sum/Typed.hs+12−1
@@ -81,13 +81,24 @@ class AsType a s where
8181 -- Just 2
8282 _Typed :: Prism' s a
8383
84+ -- |Inject by type.
85+ injectTyped :: a -> s
86+
87+ -- |Project by type.
88+ projectTyped :: s -> Maybe a
89+
8490instance
8591 ( Generic s
8692 , ErrorUnlessOne a s (CountPartialType a (Rep s))
8793 , GAsType (Rep s) a
8894 ) => AsType a s where
8995
90− _Typed = repIso . _GTyped
96+ _Typed
97+ = repIso . _GTyped
98+ injectTyped
99+ = to . ginjectTyped
100+ projectTyped
101+ = either (const Nothing) Just . gprojectTyped . from
91102
92103type family ErrorUnlessOne (a :: Type) (s :: Type) (count :: Count) :: Constraint where
93104 ErrorUnlessOne a s 'None