Elixir v1.7.4 Macro.Env View Source

A struct that holds compile time environment information.

The current environment can be accessed at any time as __ENV__/0. Inside macros, the caller environment can be accessed as __CALLER__/0.

An instance of Macro.Env must not be modified by hand. If you need to create a custom environment to pass to Code.eval_quoted/3, use the following trick:

def make_custom_env do
  import SomeModule, only: [some_function: 2]
  alias A.B.C
  __ENV__
end

You may then call make_custom_env() to get a struct with the desired imports and aliases included.

It contains the following fields:

  • module - the current module name
  • file - the current file name as a binary
  • line - the current line as an integer
  • function - a tuple as {atom, integer}, where the first element is the function name and the second its arity; returns nil if not inside a function
  • context - the context of the environment; it can be nil (default context), inside a guard or inside a match
  • aliases - a list of two-element tuples, where the first element is the aliased name and the second one the actual name
  • requires - the list of required modules
  • functions - a list of functions imported from each module
  • macros - a list of macros imported from each module
  • macro_aliases - a list of aliases defined inside the current macro
  • context_modules - a list of modules defined in the current context
  • lexical_tracker - PID of the lexical tracker which is responsible for keeping user info

The following fields pertain to variable handling and must not be accessed or relied on. To get a list of all variables, see vars/1:

  • current_vars
  • unused_vars
  • prematch_vars
  • contextual_vars

The following fields are deprecated and must not be accessed or relied on:

  • vars - a list keeping all defined variables as {var, context}

Link to this section Summary

Functions

Checks if a variable belongs to the environment

Returns whether the compilation environment is currently inside a guard

Returns whether the compilation environment is currently inside a match clause

Returns a keyword list containing the file and line information as keys

Returns the environment stacktrace

Returns a Macro.Env in the match context

Returns a list of variables in the current environment

Link to this section Types

Link to this type aliases() View Source
aliases() :: [{module(), module()}]
Link to this type context() View Source
context() :: :match | :guard | nil
Link to this type context_modules() View Source
context_modules() :: [module()]
Link to this type functions() View Source
functions() :: [{module(), [name_arity()]}]
Link to this type lexical_tracker() View Source
lexical_tracker() :: pid() | nil
Link to this type macro_aliases() View Source
macro_aliases() :: [{module(), {integer(), module()}}]
Link to this type macros() View Source
macros() :: [{module(), [name_arity()]}]
Link to this type name_arity() View Source
name_arity() :: {atom(), arity()}
Link to this type requires() View Source
requires() :: [module()]
Link to this type t() View Source
t() :: %Macro.Env{
  module: atom(),
  file: file(),
  line: line(),
  function: name_arity() | nil,
  context: context(),
  requires: requires(),
  aliases: aliases(),
  functions: functions(),
  macros: macros(),
  macro_aliases: aliases(),
  context_modules: context_modules(),
  vars: vars(),
  unused_vars: unused_vars(),
  current_vars: current_vars(),
  prematch_vars: prematch_vars(),
  lexical_tracker: lexical_tracker(),
  contextual_vars: contextual_vars()
}

Link to this section Functions

Link to this function has_var?(env, var) View Source (since 1.7.0)
has_var?(t(), var()) :: boolean()

Checks if a variable belongs to the environment.

Link to this function in_guard?(env) View Source
in_guard?(t()) :: boolean()

Returns whether the compilation environment is currently inside a guard.

Link to this function in_match?(env) View Source
in_match?(t()) :: boolean()

Returns whether the compilation environment is currently inside a match clause.

Link to this function location(env) View Source
location(t()) :: keyword()

Returns a keyword list containing the file and line information as keys.

Link to this function stacktrace(env) View Source
stacktrace(t()) :: list()

Returns the environment stacktrace.

Link to this function to_match(env) View Source
to_match(t()) :: t()

Returns a Macro.Env in the match context.

Link to this function vars(env) View Source (since 1.7.0)
vars(t()) :: [var()]

Returns a list of variables in the current environment.

Each variable is identified by a tuple of two elements, where the first element is the variable name as an atom and the second element is its context, which may be an atom or an integer.