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. InfluxDB 基本认识

    一.InfluxDB 简介 InfluxDB 是用Go语言编写的一个开源分布式时序.事件和指标数据库,无需外部依赖.类似的数据库有Kairosdb.OpenTsdb等. 三大特性: 时序性(Time ...

  2. (转)C#连接Oracle数据库(直接引用dll使用)

    原文地址:http://www.cnblogs.com/gguozhenqian/p/4262813.html 项目中有个功能需要从一台Oracle数据库获取数据,本以为是很简单的事情,直接将原来的S ...

  3. C++ Templates编程(模板参数)

    //file max.hpp template <typename T> //template<class T> inline T const& max (T cons ...

  4. solr 使用edismax来控制评分

    如何控制评分 如果设置了sort字段,那么将会按照sort字段的顺序返回结果. 如果没有设置sort字段,那么将会根据相关度打分来排序.也就是说,相关度更高的排在前面. 如何来定制适合自身业务的排序打 ...

  5. solr7.1.0学习笔记(10)---Solr发布到Tomcat

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/weixin_39082031/article/details/79069554 将solr作为一个单 ...

  6. 浅析Redis 和MongoDB

    今天来聊聊什么事nosql,一听nosql也许很多人会觉得很高大上的感觉,但其实接触过了也还觉得还行,随着当今数据的疯狂爆炸性的增长,传统的RDBMS也越来越暴露出他的不足之处,所以,作为一名合格的程 ...

  7. css(层叠样式表)属性

    CSS属性相关 宽和高 width属性可以为元素设置宽度. height属性可以为元素设置高度. 块级标签才能设置宽度,内联标签的宽度由内容来决定. 字体属性 文字字体 font-family可以把多 ...

  8. linux下mysql-5.6忘记root密码,重置root密码详细过程

      在linux平台下使用mysql过程中忘记了root密码,对于运维和DBA来讲都是一件头疼的事情,下面来讲解下怎么进行重置mysql数据库root 密码: 1.首先停止mysql服务进程: 1 s ...

  9. android开发 RecyclerView 瀑布列表布局

    1.写一个内容的自定义小布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xm ...

  10. [UnityShader基础]01.渲染队列

    unity中定义了5个渲染队列: 1.Background,对应索引号1000,该队列最先被渲染 2.Geometry,对应索引号2000,默认的渲染队列,大多数物体都使用该队列,不透明物体使用该队列 ...