Haskell语言学习笔记(81)Data.Typeable

Data.Typeable

利用 Data.Typeable,可以打印动态类型信息。

class Typeable (a :: k) where
  typeRep# :: TypeRep a

typeRep :: Typeable a => TypeRep a
typeRep = typeRep#

typeOf :: Typeable a => a -> TypeRep a
typeOf _ = typeRep

typeOf 函数可以返回某个值的类型信息。

Prelude> :m +Data.Typeable
Prelude Data.Typeable> :t typeOf 'a'
typeOf 'a' :: TypeRep
Prelude Data.Typeable> typeOf 'a'
Char

How can I read the metadata of a type at runtime?

猜你喜欢

转载自www.cnblogs.com/zwvista/p/9297291.html