Pantrypoints Pantrypoints

Macros

August 22, 2023 1 minutes  • 157 words
Table of contents

Elixir is based on functions in functions in functions.

Functions have a complicated syntax. To simplify them, the functions are organized into “macros” by defmacro.

Macros are evaluated at compile time and must return quote expression.

These are then used to define constants and DSLs. But macros are really just reused functions.

Quote

quote substitutes {function, metadata, arguments} usually {Expression, Modules, Nested Expressions}

quote translates whatever is in it as AST which has precedence.

quote do 
  1 + (2 - 3)
end

is translated as AST:

{:+, [...], [1, {:-, [...], [2,3]}]}

quote do: variable translates to `{:variable, [], Elixir}``

Quoted literals return themselves

quote do: "hey"
# "hey"

Unquote

String interpolation inside quotes

n = 12

quote do: 1 + unquote(n)
# {:+, [context: Elixir, imports: [{1, Kernel}, {2, Kernel}]], [1, 12]}

Macro.to_string – converts the long macro to its string version

Defmacro

defmacro my_if do

end

defmacro using(which) when is_atom(which) do apply(MODULE, which, []) end

Follow Us! →

We're creating a new Economic System from a new Economic Science! Please support us by leaving your email or leaving a comment above.