on main help sign in
Replace Dynamic to Any in Map
commit6a84c8566f049377617d0f302761b2bc8839f6f3 authorvrom911 <vrom911@gmail.com> dateSat 16 Dec 2017, 14:26 (9 years ago) parent0f3b672
1 file changed+6−6
src/Data/TypeRep/Map.hs+6−6
src/Data/TypeRep/Map.hs+6−6
@@ -10,17 +10,17 @@ module Data.TypeRep.Map
1010
1111import Prelude hiding (lookup)
1212
13−import Control.Monad ((<=<))
14−import Data.Dynamic (Dynamic, fromDynamic, toDyn)
1513import Data.Proxy (Proxy (..))
1614import Data.Typeable (TypeRep, Typeable, typeOf, typeRep)
15+import GHC.Base (Any)
16+import Unsafe.Coerce (unsafeCoerce)
1717
1818import qualified Data.Map.Lazy as LMap
1919
2020-- | Map-like data structure with types served as the keys.
2121newtype TypeRepMap = TypeRepMap
22− { unMap :: LMap.Map TypeRep Dynamic
23− } deriving (Show)
22+ { unMap :: LMap.Map TypeRep Any
23+ }
2424
2525-- | Empty structure.
2626empty :: TypeRepMap
@@ -28,7 +28,7 @@ empty = TypeRepMap mempty
2828
2929-- | Inserts the value with its type as a key.
3030insert :: 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
3232
3333-- | Looks up the value at the type.
3434-- >>> let x = lookup $ insert (11 :: Int) empty
@@ -37,4 +37,4 @@ insert val = TypeRepMap . LMap.insert (typeOf val) (toDyn val) . unMap
3737-- >>> x :: Maybe ()
3838-- Nothing
3939lookup :: 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