[Haskell-cafe] A newbie question ?
zaxis
z_axis at 163.com
Tue Apr 27 23:15:07 EDT 2010
"proxy type" is indeed much clear! thanks
Ryan Ingram wrote:
>
> 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 <z_axis at 163.com> 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
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-----
fac n = let { f = foldr (*) 1 [1..n] } in f
--
View this message in context: http://old.nabble.com/A-newbie-question---tp28383563p28384431.html
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
More information about the Haskell-Cafe
mailing list