• 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 函数可以返回某个值的类型信息。

    {-# LANGUAGE DeriveDataTypeable, GADTs #-}
    import Data.Typeable
    import Data.Data
    data Person = Person { name :: String, age :: Int } deriving (Typeable, Data)
    
    {-
    *Main> typeOf (undefined :: Person)
    Person
    *Main> tyConName $ typeRepTyCon $ typeOf (undefined :: Person)
    "Person"
    *Main> tyConModule $ typeRepTyCon $ typeOf (undefined :: Person)
    "Main"
    *Main> dataTypeOf (undefined :: Person)
    DataType {tycon = "Person", datarep = AlgRep [Person]}
    *Main> dataTypeConstrs $ dataTypeOf (undefined :: Person)
    [Person]
    *Main> constrFields $ head $ dataTypeConstrs $ dataTypeOf (undefined :: Person)
    ["name","age"]
    -}
    
    f :: Typeable a => a -> String
    f x = case (cast x :: Maybe Int) of
               Just i -> "I can treat i as an int in this branch " ++ show (i * i)
               Nothing -> case (cast x :: Maybe Bool) of
                               Just b -> "I can treat b as a bool in this branch " ++ if b then "yes" else "no"
                               Nothing -> "x was of some type other than Int or Bool"
    
    {-
    *Main> f True
    "I can treat b as a bool in this branch yes"
    *Main> f (3 :: Int)
    "I can treat i as an int in this branch 9"
    -}
    
    data Admissible a where
        AdInt :: Admissible Int
        AdBool :: Admissible Bool
    f2 :: Admissible a -> a -> String
    f2 AdInt i = "I can treat i as an int in this branch " ++ show (i * i)
    f2 AdBool b = "I can treat b as a bool in this branch " ++ if b then "yes" else "no"
    
    {-
    *Main> f2 AdInt 3
    "I can treat i as an int in this branch 9"
    *Main> f2 AdBool True
    "I can treat b as a bool in this branch yes"
    -}
    

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

  • 相关阅读:
    eclipse maven项目 热部署
    二十三. Django ModelForm组件
    二十二 .Django生命周期
    二十二 .Django form上传+Ajax+FormData上传+Form组件上传+序列化+ifram伪造Ajax
    二十一. Django----ajax全部
    二十. Django ajax--请求数据 和模态对话框
    二十. Django分页 和自定义分页
    十九. Django-Cookie的CBV和FBV的用户验证装饰器
    十八 .Django session应用
    十七 .Django cookie应用
  • 原文地址:https://www.cnblogs.com/zwvista/p/9297291.html
Copyright © 2020-2023  润新知