Georgy Savva
Georgy Savva

Reputation: 689

Elixir: required inside a macro

I wrote a macro, and I want to use Logger.info in it, so i have to require Logger. What is the best practice to do this? Oblige the client to require Logger on its own, or add using macro and require Logger there.

Upvotes: 2

Views: 402

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

The best practice would be not to log from your macros all of a sudden. As a user of your macro, I might do not want to have my log polluted with irrelevant information. On the other hand, that might be a significant to have such an ability opt-in. That said, I would go with a parametrized __using__:

defmodule My do
  defmacro __using__(opts \\ [log: false]) do
    quote do
      if unquote(opts)[:log], do: require Logger

      defmacro my_log(what) do
        if unquote(opts)[:log] do
          Logger.info(what)
        end 
      end

      ...
    end
  end
end

Now you can safely call my_log when needed and if the user did not explicitly require the ability to log via params passed to use:

use My, log: true

no single trace of Logger will pollute the resulting code.

Upvotes: 1

Mike Buhot
Mike Buhot

Reputation: 4885

Does the Logger call need to be inside the macro? Perhaps the macro can expand to a function call to a module that then does the logging.

If it is really necessary to perform the logging from the macro expansion, then I'd add a __using__ macro to require dependencies and your module.

Upvotes: 0

Related Questions