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

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

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

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

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

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

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

  1. iex(5)> defimpl IEx.Info, for: User do
  2. …(5)> def info(item) do
  3. …(5)> ["Data type": User, "Description": "The customer is god, pleasure they", "Reference": "blablabla..."]
  4. …(5)> end
  5. …(5)> end
  6. iex(6)> i %User{}
  7. Term
  8. %User{age: 25, name: "John"}
  9. Data type
  10. Elixir.User
  11. Description
  12. The customer is god, pleasure they
  13. Reference
  14. blablabla...

成功!

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

彩蛋:

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

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

  1. IEx.dont_display_result

在 evaluator.ex 里面:

  1. 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. 处理DateTime.Now不经过ToString()转换的格式(带有AM、PM)问题

    问题是这样的: DateTime.Now不经过ToString()转换,网站部署到测试服务器(国内)得到的时间格式是:2018/8/17 16:26:09,而部署到国外服务器得到的时间格式是:17/8 ...

  2. C++11奇怪的语法

    1. istream_iterator 简而言之,istream_iterator像操作容器一样操作istream.例如下面代码,从std::cin构造std::istream_iteream< ...

  3. 利用IOS画图功能画出五角星,并且可以调整五角星的填充范围

    我们要花的为一个黄色的五角星并且其中的填充黄色能够任意调整,比如只填满半个五角星,或者只填满一个角等等. 首先要重写DrawRect 方法,然后在这里实现我们的画图代码. - (void)drawRe ...

  4. ggplot2 texts : Add text annotations to a graph in R software

    http://www.sthda.com/english/wiki/ggplot2-texts-add-text-annotations-to-a-graph-in-r-software Instal ...

  5. django-DIL模板自定义过滤器,自定义标签,自定义包含标签

    自定义过滤器 DTL模板语言生来只是为了方便的展示信息,所以与编程语言相比显得有点薄弱,有时候不能满足我们的需求.因此django提供了一个接口,让开发者能自定义标签和过滤器. 首先,你需要添加一个t ...

  6. Rhythmk 一步一步学 JAVA (19): 注解 annotation

    在编写注解的时候需要了解的四种注解: @Target 表示该注解可以用于什么地方,可能的ElementType参数有: CONSTRUCTOR:构造器的声明 FIELD:域声明(包括enum实例) L ...

  7. leetcode257

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  8. leetcode303

    public class NumArray { List<int> list = new List<int>(); public NumArray(int[] nums) { ...

  9. leetcode112

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  10. 埃氏筛法求素数&构造素数表求素数

    埃氏筛法求素数和构造素数表求素数是一个道理. 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1 ...