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?

Haskell语言学习笔记(81)Data.Typeable的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  3. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  4. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  5. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  6. Haskell语言学习笔记(28)Data.Map

    Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> Overlo ...

  7. Haskell语言学习笔记(93)Data.Text

    Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text ...

  8. Haskell语言学习笔记(77)Data.HashSet

    安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...

  9. Haskell语言学习笔记(76)Data.Tree

    Data.Tree data Tree a = Node { rootLabel :: a, subForest :: Forest a } deriving (Eq, Read, Show) typ ...

随机推荐

  1. JavaWeb中四大域对象的作用范围

    JavaWeb的四大作用域为:PageContext,ServletRequest,HttpSession,ServletContext: PageContext域:作用范围是整个JSP页面,是四大作 ...

  2. implementation compile的区别

    implementation不可以依赖传递:依赖对app Module 是不可见的 compile可以依赖传递:依赖对app Module 是可见的 AndroidStudio升级到3.0以上后,第一 ...

  3. sql中的in与not in,exists与not exists的区别

    1.in和exists in是把外表和内表作hash连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询,一直以来认为exists比in效率高的说法是不准确的.如果查询的两个表 ...

  4. flask 简单的语音识别

    from aip import AipSpeech,AipNlp #AipNlp 为自然语言处理 """ 你的 APPID AK SK """ ...

  5. 爬虫概念 requests模块

    requests模块 - 基于如下5点展开requests模块的学习 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能 ...

  6. Python: json模块实例详解

    ref:https://www.jianshu.com/p/e29611244810 https://www.cnblogs.com/qq78292959/p/3467937.html https:/ ...

  7. vuejs实现瀑布流布局(二)

    瀑布流布局已然完成,那么剩下的就是另一个比较大的工程了——无限加载. 之前说了,这个活动项目是基于SUI-Mobile搭建的,所以可以直接使用sui内建组件“无限加载”来实现这个功能. 没有真实的数据 ...

  8. 零基础学习python_pickle(31课)

    上次我提到了对文件的读写等一系列操作,回想下,要想从文件内读取内容无论是read还是readline,读取出来的是不是都是字符串呢?那么如果想让字典.列表这些数据类型保存进文件到读取出来都是原来的类型 ...

  9. AJAX发送 PUT和DELETE请求参数传递注意点,了解一下

    ajax发送put 和 delete 请求时,需要传递参数,如果参数在url地址栏上,则可以正常使用, 如果在 data:中需要传递参数,(浏览器会使用表单提交的方式进行提交) 则需要注意此时应作如下 ...

  10. CentOS7上安装RabbitMQ

    1.首先需要安装erlang RabbitMq依赖于erlang,所以得先装这个.推荐使用阿里云的镜像资源. 通过这个链接 https://opsx.alibaba.com/mirror/search ...