Delegate v0.2.0 Delegate.Macro View Source

Provides a defmacrodelegate which allows delegating a macro to another macro at compile time. In addition, defmacrodelegateall is provided which allows delegating to all the macros on the target module

use Delegate.Macro provides all macros (require + import)

Link to this section Summary

Functions

Delegates a macro to another macro at compile time

Creates macro delegates for each macro on the :to module

Link to this section Types

Link to this type

defmacrodelegate_opts() View Source
defmacrodelegate_opts() :: {:as, atom()} | {:to, module()}

Link to this type

defmacrodelegateall_opts() View Source
defmacrodelegateall_opts() ::
  {:only, [{atom(), arity()}]} | {:except, [{atom(), arity()}]}

Link to this section Functions

Link to this macro

defmacrodelegate(head, opts) View Source (macro)
defmacrodelegate(head :: term(), opts :: [defmacrodelegate_opts()]) :: term()

Delegates a macro to another macro at compile time.

Arguments

  • head is the macro definition
  • opts is a keyword:

    • :to (required) which refers to the module the macro must be delegated to
    • :as (optional) a different name for the macro delegating to the module. When not specified, it defaults to the macro name defined in :to module

Examples

defmodule BaseMacro do
  defmacro hello(name) do
    quote do
      "hello #{unquote(name)}"
    end
  end
end

defmodule DelegateMacro do
  use Delegate.Macro

  defmacrodelegate hello(name), to: BaseMacro, as: :world
end

defmodule UseTheMacro do
  require DelegateMacro

  def hello_func do
    DelegateMacro.hello("Francesco") # => "hello Francesco"
  end
end
Link to this macro

defmacrodelegateall(to, opts \\ []) View Source (macro)
defmacrodelegateall(to :: module(), opts :: [defmacrodelegateall_opts()]) ::
  term()

Creates macro delegates for each macro on the :to module

Arguments

  • to module to delegate to and to find the list of macros to delegate
  • opts is a keyword:

    • :only (optional) macros that will be delegated, excluding everything else
    • :except (optional) all macros will be delegated except those listed in this argument

Examples

defmodule BaseMacro do
  defmacro hello(name) do
    quote do
      "hello #{unquote(name)}"
    end
  end

  defmacro bye() do
    quote do
      "bye"
    end
  end
end

defmodule DelegateMacro do
  use Delegate.Macro

  defmacrodelegateall(BaseMacro)

  # Macro `hello/1` and `bye/0` are defined in this module
end

require DelegateMacro

DelegateMacro.hello("Jon")
DelegateMacro.bye()