LoginSignup
11
11

More than 5 years have passed since last update.

Elixir v1.2-rc のi/1ヘルパーを使ってみた

Last updated at Posted at 2015-12-13

はじめに

この記事はElixir Advent Calendar 2015の13日目の記事です。

私 is 誰?
hayabusa333 という名前でElixirの変更点やPhoenixの変更点をブログにまとめたり、週刊Elixirライブラリをまとめたりしている人です。
最近はどれもちゃんと更新できていなくて申し訳ありません。

Elixir v1.2-rc リリース

Elixir v1.2-rc が2015年12月10日にリリースされました。
Elixir v1.2-rc の詳しい内容については、nikuさんが書かれたChangelog for Elixir v1.2の一部を翻訳してみました.を読まれるか
GithubのChangelogを読んでいただければと思います。

i/1ヘルパーとは

i/1ヘルパーとは、IEx.Info機能とも言われるものですが始まりは11月23日から作成が入りました。
プルリクはこちら → プルリク#3934
作成者は、whatyouhide氏José Valim氏というのが私の観測範囲内での認識です。

実際に実行してみた

実行するためにはGithubから最新のソースコードをとってきてビルドすれば可能です。
Elixirのソースコードをビルドはそれほど難しくないので説明は特に記載いたしません。

では、Elixir v1.2-rcの IExを実行していきましょう。
i/1ヘルパーはIEx用のヘルパーですので、IExで確認する内容です。
まずは、Atom と Integerを調べてみます。

## Atom型の情報を確認する
iex(1)> i Atom
Term
  Atom
Data type
  Atom
Module bytecode
  bin/../lib/elixir/ebin/Elixir.Atom.beam
Source
  lib/elixir/lib/atom.ex
Version
  [155945150764402944199015540540637396633]
Compile time
  2015-12-12 0:11:0
Compile options
  [:debug_info]
Description
  Use h(Atom) to access its documentation.
  Call Atom.module_info() to access metadata.
Raw representation
  :"Elixir.Atom"
Reference modules
  Module, Atom

## Integer型の情報を確認する
iex(2)> i Integer
Term
  Integer
Data type
  Atom
Module bytecode
  bin/../lib/elixir/ebin/Elixir.Integer.beam
Source
  lib/elixir/lib/integer.ex
Version
  [286685332190039663504791160081505036739]
Compile time
  2015-12-12 0:11:16
Compile options
  [:debug_info]
Description
  Use h(Integer) to access its documentation.
  Call Integer.module_info() to access metadata.
Raw representation
  :"Elixir.Integer"
Reference modules
  Module, Atom

かなり情報が出力されてきますね。

では、続いて自分自身が定義したモジュールはどのような形で出力されるのか見ていきましょう。

## 自分で作成したモジュールの情報を確認する
iex(3)> defmodule Foo do
...(3)> end
{:module, Foo,
 <<70, 79, 82, 49, 0, 0, 3, 152, 66, 69, 65, 77, 69, 120, 68, 
 99, 0, 0, 0, 94, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 
 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 
 104, 2, ...>>,
 nil}
iex(4)> i Foo
Term
  Foo
Data type
  Atom
Module bytecode
  :in_memory
Source
  iex
Version
  [140921964942447222808887230990124110529]
Compile time
  2015-12-12 0:33:52
Compile options
  [:debug_info]
Description
  Call Foo.module_info() to access metadata.
Raw representation
  :"Elixir.Foo"
Reference modules
  Module, Atom

iex上で定義されたものであることや in_memory に登録されている内容であることも表示されています!

では、パターンマッチされて束縛された値はどのように表示されるのか見ていきます。

## データ型をパターンマッチさせた変数の情報を確認する
iex(5)> value = 1
1
iex(6)> i value
Term
  1
Data type
  Integer
Reference modules
  Integer

iex(7)> str = "Hoge"
"Hoge"
iex(8)> i str
Term
  "Hoge"
Data type
  BitString
Byte size
  4
Description
  This is a string: a UTF-8 encoded binary. It's printed surrounded by
  "double quotes" because all UTF-8 codepoints in it are printable.
Raw representation
  <<72, 111, 103, 101>>
Reference modules
  String, :binary

iex(9)> user = %{:name => "test"}
%{name: "test"}
iex(10)> i user
Term
  %{name: "test"}
Data type
  Map
Reference modules
  Map
iex(11)>
//}

こちらの紹介した理由は、最近ElixirとPhoenixの薄い本を書いていてElixir v1.1で本の内容を書いていたら、Elixir v1.2-rcがリリースされて、どうしようか考えていた際に調べた内容となります。

そして調べた中で一番即応性が高くて、一番驚いた内容なため今回Advent Calendarで紹介させていただきました!

本には他の v1.2-rcの内容も記載されていますが、それはここでの話ではないですね。

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11