elixir在1.2后增加了一个新的特性i helper. 在iex shell中使用i可以查看任意数据的数据类型和详细描述
#查看变量描述
iex(1)> i {:test, "That sounds great"}
Term
{:test, "That sounds great"}
Data type
Tuple
Reference modules
Tuple
#查看Module描述(有点类似于Erlang的lists:module_info)
iex(2)> i List
Term
List
Data type
Atom
Module bytecode
usr/local/Cellar/elixir/1.2.0/bin/../lib/elixir/ebin/Elixir.List.beam
Source
private/tmp/elixir20160101-48495-1fg1arr/elixir-1.2.0/lib/elixir/lib/list.ex
Version
[322093417650371381585336045669056703278]
Compile time
2016-1-1 11:57:45
Compile options
[:debug_info]
Description
Use h(List) to access its documentation.
Call List.module_info() to access metadata.
Raw representation
:"Elixir.List"
Reference modules
Module, Atom
来看看这么神奇的功能是怎么实现的吧~

  @doc """
Prints information about the given data type.
"""
def i(term) do
info = ["Term": inspect(term)] ++ IEx.Info.info(term) for {subject, info} <- info do
info = info |> to_string() |> String.strip() |> String.replace("\n", "\n ")
IO.puts IEx.color(:eval_result, to_string(subject))
IO.puts IEx.color(:eval_info, " #{info}")
end dont_display_result
end
可以看出它只是把IEx.info.info的结果打出来,我们再看看它发生了什么?
defprotocol IEx.Info do
@fallback_to_any true @spec info(term) :: [{atom, String.t}]
def info(term)
end defimpl IEx.Info, for: Tuple do
def info(_tuple) do
["Data type": "Tuple",
"Reference modules": "Tuple"]
end
end

它是一个protocol,在这个文件中把elixir的基本类型都实现了一次,它会返回一个keyword list, 所以我们才能看到,那么如果我们试试自己定义?

iex(3)> defmodule User do
…(3)> defstruct name: "John", age: 25
…(3)> @type t :: %User{name: String.t, age: non_neg_integer}
…(3)> end

因为在info.ex中已处理struct的类型, 如果我们现在直接i的结果它是

iex(4)> i %User{}
Term
%User{age: 25, name: "John"}
Data type
User
Description
This is a struct. Structs are maps with a __struct__ key.
Reference modules
User, Map

接下来, 我们来自定义看看

iex(5)> defimpl IEx.Info, for: User do
…(5)> def info(item) do
…(5)> ["Data type": User, "Description": "The customer is god, pleasure they", "Reference": "blablabla..."]
…(5)> end
…(5)> end
iex(6)> i %User{}
Term
%User{age: 25, name: "John"}
Data type
Elixir.User
Description
The customer is god, pleasure they
Reference
blablabla...

成功!

官方文档: http://elixir-lang.org/docs/stable/iex/IEx.Helpers.html#i/1

彩蛋:

有没有看到我们输入i得到的结果,只是把格式用有颜色的格式打印出来,但是确没有看到返回值被打印出来。。。

它的结果无论如何都打印不出来滴。因为它调用了

IEx.dont_display_result

在 evaluator.ex 里面:

 unless result == IEx.dont_display_result, do: io_inspect(result)

if my fingers were erlang processes

[Elixir005] 查看指定数据的详细信息 i helper的更多相关文章

  1. asp.net/wingtip/显示数据和详细信息

    前边我们的工作处于wingtip工程基础建设阶段,先是建立了“数据访问层”,然后设计建设了“UI和导航”的框架,接下来要充实工程的内容,显示“数据和详细信息”. 一. 添加数据控件(Data Cont ...

  2. 操作系统复习——如何查看一个进程的详细信息,如何追踪一个进程的执行过程 ,如何在 Linux 系统下查看 CPU、内存、磁盘、IO、网卡情况?epoll和select区别?

    1. 如何查看一个进程的详细信息,如何追踪一个进程的执行过程 通过pstree命令(根据pid)进行查询进程内部当前运行了多少线程:# pstree -p 19135(进程号) 使用top命令查看(可 ...

  3. 使用tcpdump查看HTTP请求响应 详细信息 数据

    安装tcpdump: sudo yum install tcpdump 查看get请求: tcpdump -s 0 -A 'tcp dst port 80 and tcp[((tcp[12:1] &a ...

  4. 【数据库】】MySQL之desc查看表结构的详细信息

    在mysql中如果想要查看表的定义的话:有如下方式可供选择 1.show create table 语句: show create table table_name; 2.desc table_nam ...

  5. MySQL之desc查看表结构的详细信息

    在mysql中如果想要查看表的定义的话:有如下方式可供选择 1.show create table 语句: show create table table_name; 2.desc table_nam ...

  6. Python查看模块函数,查看函数方法的详细信息

    Python查看方法的详情 1.通用的帮助函数help() 使用help()函数来查看函数的帮助信息. 如: import requests help(requests) 会有类似如下输出: 2.查询 ...

  7. 查看mssql死锁的详细信息(存储过程)

    CREATE  procedure [dbo].[sp_who_lock]asbegindeclare @spid int,@bl int,        @intTransactionCountOn ...

  8. docker查看容器元数据、详细信息,查看容器挂载的目录

    通过 docker inspect 175f 查看容器元数据 我们启动docker的时候会挂载目录,但是挂载之后 后面就忘了 如何查看挂载的目录位置呢 可以通过 docker inspect a7a6 ...

  9. linux下如何查看某个容器的详细信息?

    答: 使用docker inspect <CONTAINER ID>即可

随机推荐

  1. 【POJ】2778 DNA Sequence(AC自动机+矩阵快速幂)

    题目 传送门:QWQ 分析 对着Trie图搞快速幂. 为什么这样是对的呢? 详见:http://www.matrix67.com/blog/archives/276 有些地方还不是很理解......为 ...

  2. Linux 性能分析的前 60 秒

    编译自:http://techblog.netflix.com/2015/11/linux-performance-analysis-in-60s.html作者: Brendan Gregg转载自:h ...

  3. solr统计只返回10或者100个数据的解决办法

    因为我所在的公司为政府做的项目[风险管理系统],其中涉及大量的统计展示,多数以整个市的区划,行业部门等方式返回,在昨天,我发现听过填报单位的方式返回时,始终只有100个数据.通过对比发现,在前辈的代码 ...

  4. JSF + Primefaces: Problem with “rendered” components with ajax

    Cant seem to get rendered to work correctly with update attributes. Here is my codes <ui:define n ...

  5. C#预编译的问题

    C#预编译宏并不像C++那样编译之后就不存在了.在UNITY的C#脚本中 #if UNITY_ANDROID && !UNITY_EDITOR AndroidJavaClass jc ...

  6. 迷你MVVM框架 avalonjs 学习教程9、类名操作

    ms-class是avalon用得最多的几个绑定之一,也正因为如此其功能一直在扩充中.根据时期的不同,分为旧风格与新风格两种. 旧风格是指ms-class-xxx=”expr”,*ms-class-a ...

  7. Spring-JDBC模板-事务

    Spring-JDBC模板-事务 1.事务概述 什么是事务 逻辑上的一组操作,组成这组操作的各个单元要么全部成功要么全部失败 事务的特点ACID 原子性:事务不可分割(事务要么成功,要么失败) 一致性 ...

  8. S 导客户主数据 及更新销售团队、组织、品牌

    一.导入客户主表(INSERT) EXCEL模板 [Public] ConnectString=host="siebel://10.10.0.46:2321/HC_CRM/SMObjMgr_ ...

  9. Android Studio连接真机

    -------------siwuxie95         1.首先创建一个项目:HelloWorld,点击app,出现下拉选项,选择Edit Configurations             ...

  10. 27-python 画图

    绝佳教程:http://pyecharts.org/#/zh-cn/prepare?id=%E4%BD%BF%E7%94%A8%E4%B8%BB%E9%A2%98安装 pyecharts pip in ...