1-- |
2-- Module: Data.Typeable.Optics
3-- Description: Optics for working with 'Typeable'.
4--
5module Data.Typeable.Optics
6  ( _cast
7  , _gcast
8  ) where
9
10import Data.Typeable
11import Data.Maybe
12
13import Optics.AffineTraversal
14
15-- | An 'AffineTraversal'' for working with a 'cast' of a 'Typeable' value.
16_cast :: (Typeable s, Typeable a) => AffineTraversal' s a
17_cast = atraversalVL $ \point f s -> case cast s of
18  Just a  -> fromMaybe (error "_cast: recast failed") . cast <$> f a
19  Nothing -> point s
20{-# INLINE _cast #-}
21
22-- | An 'AffineTraversal'' for working with a 'gcast' of a 'Typeable' value.
23_gcast :: (Typeable s, Typeable a) => AffineTraversal' (c s) (c a)
24_gcast = atraversalVL $ \point f s -> case gcast s of
25  Just a  -> fromMaybe (error "_gcast: recast failed") . gcast <$> f a
26  Nothing -> point s
27{-# INLINE _gcast #-}
28