On this page
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:
module- the current module namefile- the current file name as a binaryline- the current line as an integerfunction- a tuple as{atom, integer}, where the first element is the function name and the second its arity; returnsnilif not inside a functioncontext- the context of the environment; it can benil(default context), inside a guard or inside a matchaliases- a list of two-element tuples, where the first element is the aliased name and the second one the actual namerequires- the list of required modulesfunctions- a list of functions imported from each modulemacros- a list of macros imported from each modulemacro_aliases- a list of aliases defined inside the current macrocontext_modules- a list of modules defined in the current contextlexical_tracker- PID of the lexical tracker which is responsible for keeping user infovars- a list keeping all defined variables as{var, context}
The following fields are private and must not be accessed or relied on:
export_vars- a list keeping all variables to be exported in a construct (may benil)match_vars- controls how “new” variables are handled. Inside a match it is a list with all variables in a match. Outside of a match is either:warnor:applyprematch_vars- a list of variables defined before a match (isnilwhen not inside a match)
Summary
Types
- aliases()
- context()
- context_modules()
- export_vars()
- file()
- functions()
- lexical_tracker()
- line()
- local()
- macro_aliases()
- macros()
- match_vars()
- name_arity()
- prematch_vars()
- requires()
- t()
- vars()
Functions
- 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.Envin the match context
Types
aliases()
aliases() :: [{module(), module()}]
context()
context() :: :match | :guard | nil
context_modules()
context_modules() :: [module()]
export_vars() (opaque)
export_vars()
file()
file() :: binary()
functions()
functions() :: [{module(), [name_arity()]}]
lexical_tracker()
lexical_tracker() :: pid() | nil
line()
line() :: non_neg_integer()
local()
local() :: atom() | nil
macro_aliases()
macro_aliases() :: [{module(), {integer(), module()}}]
macros()
macros() :: [{module(), [name_arity()]}]
match_vars() (opaque)
match_vars()
name_arity()
name_arity() :: {atom(), arity()}
prematch_vars() (opaque)
prematch_vars()
requires()
requires() :: [module()]
t()
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(),
export_vars: export_vars(),
match_vars: match_vars(),
prematch_vars: prematch_vars(),
lexical_tracker: lexical_tracker()
}
vars()
vars() :: [{atom(), atom() | non_neg_integer()}]
Functions
in_guard?(env)
in_guard?(t()) :: boolean()
Returns whether the compilation environment is currently inside a guard.
in_match?(env)
in_match?(t()) :: boolean()
Returns whether the compilation environment is currently inside a match clause.
location(env)
location(t()) :: keyword()
Returns a keyword list containing the file and line information as keys.
stacktrace(env)
stacktrace(t()) :: list()
Returns the environment stacktrace.
to_match(env)
to_match(t()) :: t()
Returns a Macro.Env in the match context.
© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.6.6/Macro.Env.html