W3cubDocs

/Elixir 1.11

Macro.Env

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:

  • aliases - a list of two-element tuples, where the first element is the aliased name and the second one the actual name
  • context - the context of the environment; it can be nil (default context), :guard (inside a guard) or :match (inside a match)
  • context_modules - a list of modules defined in the current context
  • file - the current file name as a binary
  • 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
  • functions - a list of functions imported from each module
  • line - the current line as an integer
  • macro_aliases - a list of aliases defined inside the current macro
  • macros - a list of macros imported from each module
  • module - the current module name
  • requires - the list of required modules

The following fields are private to Elixir's macro expansion mechanism and must not be accessed directly:

  • contextual_vars
  • current_vars
  • lexical_tracker
  • prematch_vars
  • tracers
  • unused_vars

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

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

Summary

Types

t()

Functions

has_var?(env, var)

Checks if a variable belongs to the environment.

in_guard?(env)

Returns whether the compilation environment is currently inside a guard.

in_match?(env)

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

location(env)

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

stacktrace(env)

Returns the environment stacktrace.

to_match(env)

Returns a Macro.Env in the match context.

vars(env)

Returns a list of variables in the current environment.

Types

aliases()

Specs

aliases() :: [{module(), module()}]

context()

Specs

context() :: :match | :guard | nil

context_modules()

Specs

context_modules() :: [module()]

file()

Specs

file() :: binary()

functions()

Specs

functions() :: [{module(), [name_arity()]}]

lexical_tracker()

Specs

lexical_tracker() :: pid() | nil

line()

Specs

line() :: non_neg_integer()

macro_aliases()

Specs

macro_aliases() :: [{module(), {term(), module()}}]

macros()

Specs

macros() :: [{module(), [name_arity()]}]

name_arity()

Specs

name_arity() :: {atom(), arity()}

requires()

Specs

requires() :: [module()]

t()

Specs

t() :: %Macro.Env{
  aliases: aliases(),
  context: context(),
  context_modules: context_modules(),
  contextual_vars: contextual_vars(),
  current_vars: current_vars(),
  file: file(),
  function: name_arity() | nil,
  functions: functions(),
  lexical_tracker: lexical_tracker(),
  line: line(),
  macro_aliases: macro_aliases(),
  macros: macros(),
  module: atom(),
  prematch_vars: prematch_vars(),
  unused_vars: unused_vars(),
  requires: requires(),
  tracers: tracers(),
  vars: vars()
}

variable()

Specs

variable() :: {atom(), atom() | term()}

Functions

has_var?(env, var)

Specs

has_var?(t(), variable()) :: boolean()

Checks if a variable belongs to the environment.

in_guard?(env)

Specs

in_guard?(t()) :: boolean()

Returns whether the compilation environment is currently inside a guard.

in_match?(env)

Specs

in_match?(t()) :: boolean()

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

location(env)

Specs

location(t()) :: keyword()

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

stacktrace(env)

Specs

stacktrace(t()) :: list()

Returns the environment stacktrace.

to_match(env)

Specs

to_match(t()) :: t()

Returns a Macro.Env in the match context.

vars(env)

Specs

vars(t()) :: [variable()]

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.

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.11.2/Macro.Env.html