Remember, "fromJust res" won't be evaluated unless typeOf needs it.
But it does get typechecked at compile time, and the type is used to
select which instance of typeOf to call.

Fortunately, legal instances of Typeable require typeOf to not inspect
its argument.

This is a somewhat old design; modern variations on this tend to use a
"proxy type" to make it clear that the argument's value cannot be
evaluated.  In modern Haskell, I would write it like so:

data Proxy a = Proxy
proxy :: a -> Proxy a

class Typeable a where
   typeOf :: Proxy a -> TypeRep

-- uses ScopedTypeVariables extension
lookup :: forall a. Typeable a => TypeMap -> Maybe a
lookup (TypeMap mp) = liftM (fromJust . fromDynamic) $ Map.lookup
(typeOf (Proxy :: Proxy a)) mp

  -- ryan

On Tue, Apr 27, 2010 at 5:17 PM, zaxis <[email protected]> wrote:
>
> newtype TypeMap = TypeMap (Map.Map TypeRep Dynamic)
>
> lookup :: Typeable a => TypeMap -> Maybe a
> lookup (TypeMap mp) = res
>    where res = liftM (fromJust . fromDynamic) $ Map.lookup (typeOf $
> fromJust res) mp
>
> It seems that the `res` in  `fromJust res`  has not been defined ?
>
> Sincerely!
>
> -----
> fac n = let {  f = foldr (*) 1 [1..n] } in f
> --
> View this message in context: 
> http://old.nabble.com/A-newbie-question---tp28383563p28383563.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to