Search code examples
elixir

Is there a type for ASTs?


I am wondering whether there is an Elixir @type definition for ASTs? So, when I write a function that returns an AST.

@spec example() :: ???
def example do
  quote do
    # ...
  end
end

I initially thought that ASTs are always tuples:

iex> quote do
...>   @example :A
...> end
{:@, [context: Elixir, imports: [{1, Kernel}]],
 [{:example, [context: Elixir], [:A]}]}

But it can also be a simple string:

iex> quote do
...>   "Foo"
...> end
"Foo"

Solution

  • Macro.t() which is an alias for Macro.input() is the type to denote AST (as well as macro input, which is, in turn, AST too.)