2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

elixirのrequireってなに?

Posted at

はじめに

requireというコードが登場したがイマイチ理解できずに読み飛ばしていた。
コードをキチンと理解してるとは言い難い状況だったのでrequireが何なのかあると何ができるのかをまとめる。

requireとは?

他のモジュールのマクロを使用することをElixirに伝えるためのもの

マクロとは?

メタプログラミングを可能とする。

requireがあると何が嬉しい?

importが関数を利用するのに対してマクロを使用できるようにすることができる。
メタプログラミングを可能とするため、言語を自由に拡張することができる。

コードの例

iex(1)> Integer.is_odd(3)
** (UndefinedFunctionError) function Integer.is_odd/1 is undefined or private. However, there is a macro with the same name and arity. Be sure to require Integer if you intend to invoke this macro
    (elixir 1.18.2) Integer.is_odd(3)
    iex:1: (file)
iex(1)> require Integer
Integer
iex(2)> Integer.is_odd(3)
true

結論

requireはマクロを利用できるようにする機能のこと
マクロを利用することでメタプログラミングが可能となり、言語を自由に拡張することができる。

残った疑問

コードで書いたIntegerは関数では?
だとするとわざわざrequireで呼び出す意味がないのでは?
またメタプログラミングというのは外部からインポートする性質のものでなく、requireを呼び出そうとしたユーザーが定義するものでは?

その他

疑問点に対する指摘、誤字脱字についての指摘を頂けると幸いです。
理解を促進するための指摘は大歓迎です。

参考資料

Elixir実践入門

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?