on main help sign in
Replace Dynamic to Any in Map
6a84c85vrom911
src/Data/TypeRep/Map.hs+6−6
@@ -10,17 +10,17 @@ module Data.TypeRep.Map
10
10
11import Prelude hiding (lookup)
11import Prelude hiding (lookup)
12
12
13−import Control.Monad ((<=<))
14−import Data.Dynamic (Dynamic, fromDynamic, toDyn)
15import Data.Proxy (Proxy (..))
13import Data.Proxy (Proxy (..))
16import Data.Typeable (TypeRep, Typeable, typeOf, typeRep)
14import Data.Typeable (TypeRep, Typeable, typeOf, typeRep)
15+import GHC.Base (Any)
16+import Unsafe.Coerce (unsafeCoerce)
17
17
18import qualified Data.Map.Lazy as LMap
18import qualified Data.Map.Lazy as LMap
19
19
20-- | Map-like data structure with types served as the keys.
20-- | Map-like data structure with types served as the keys.
21newtype TypeRepMap = TypeRepMap
21newtype TypeRepMap = TypeRepMap
22− { unMap :: LMap.Map TypeRep Dynamic
22+ { unMap :: LMap.Map TypeRep Any
23− } deriving (Show)
23+ }
24
24
25-- | Empty structure.
25-- | Empty structure.
26empty :: TypeRepMap
26empty :: TypeRepMap
@@ -28,7 +28,7 @@ empty = TypeRepMap mempty
28
28
29-- | Inserts the value with its type as a key.
29-- | Inserts the value with its type as a key.
30insert :: Typeable a => a -> TypeRepMap -> TypeRepMap
30insert :: Typeable a => a -> TypeRepMap -> TypeRepMap
31−insert val = TypeRepMap . LMap.insert (typeOf val) (toDyn val) . unMap
31+insert val = TypeRepMap . LMap.insert (typeOf val) (unsafeCoerce val) . unMap
32
32
33-- | Looks up the value at the type.
33-- | Looks up the value at the type.
34-- >>> let x = lookup $ insert (11 :: Int) empty
34-- >>> let x = lookup $ insert (11 :: Int) empty
@@ -37,4 +37,4 @@ insert val = TypeRepMap . LMap.insert (typeOf val) (toDyn val) . unMap
37-- >>> x :: Maybe ()
37-- >>> x :: Maybe ()
38-- Nothing
38-- Nothing
39lookup :: forall a . Typeable a => TypeRepMap -> Maybe a
39lookup :: forall a . Typeable a => TypeRepMap -> Maybe a
40−lookup = fromDynamic <=< LMap.lookup (typeRep (Proxy :: Proxy a)) . unMap
40+lookup = fmap unsafeCoerce . LMap.lookup (typeRep (Proxy :: Proxy a)) . unMap