项目作者: dimsamaras

项目描述 :
Getting started with elixir programming lang
高级语言: Elixir
项目地址: git://github.com/dimsamaras/local_elixir.git
创建时间: 2019-05-22T07:07:00Z
项目社区:https://github.com/dimsamaras/local_elixir

开源协议:

下载


local_elixir

Getting started with elixir programming lang

require vs import

When we require a module, we instruct the Elixir to hold the compilation of the current module until the required module is compiled and loaded into the compiler run-time (the Erlang VM instance where compiler is running). We can only call macro when the module is fully compiled, and available to the compiler.

Using import has the same effect but it additionally lexically imports all exported functions and macros, making it possible to write some instead of Some.some.

use

The use mechanism allows us to inject some piece of code into the caller’s context.

  1. defmodule ClientCode do
  2. # invokes the mixin
  3. use GenericCode, option_1: value_1, option_2: value_2, ...
  4. end
  5. defmodule GenericCode do
  6. # called when the module is used
  7. defmacro __using__(options) do
  8. # generates an AST that will be inserted in place of the use
  9. quote do
  10. ...
  11. end
  12. end
  13. end

This is just a replacement for something like:

  1. defmodule ClientCode do
  2. require GenericCode
  3. GenericCode.__using__(...)
  4. end

__ENV__

The code relies on __ENV special form that can be used to inject all sort of compile-time informations (e.g. line number and file) in the final AST. __ENV is a struct and whenever you use it in the code, it will be expanded in compile time to appropriate value. Hence, wherever in code we write __ENV__ .file the resulting bytecode will contain the (binary) string constant with the containing file name.