Enabling access to occasionally used (eg, tracing) functions?

This question is a bit convoluted, so please bear with me…

I have some common functions that I use occasionally for tracing and such. For example, Common.ii/2 provides a shorthand version of IO.inspect/2:

foo = bar
|> ii(:bar)

If I want to keep a trace around for later, I comment it out. To keep my code tidy, I use an import statement:

import Common, only: [ ii: 2 ]

However, if none of the traces are currently in use, this generates a warning:

warning: unused import Common

So, I have to comment out the import line:

# import Common, only: [ ii: 2 ]

My problem arises when I import more than one function, only one of which may need to be commented out:

import Common, only: [ ii: 2, str_list: 1 ]

I tried using multiple import statements, but this fails silently (only the last one takes effect):

import Common, only: [ ii: 2 ]
import Common, only: [ str_list: 1 ]

So, I’m forced to use some rather awkward code formatting:

import Common, only: [ # ii: 2,
  str_list: 1 ]

Comments? Clues? Suggestions?

use the module and return a quoted expression that is generated: true.

Thanks for the prompt response. However, after Googling a bit and trying things out, I don’t think this will work for my use case.

Problem is, my Common module (following @PragDave’s suggested approach) only contains defdelegate statements. So, when I wrap my tracing functions (in Common.Tracing) in the quote, Elixir gets very annoyed.

More generally, this approach seems a bit arcane, given the (seemingly) simple thing I’m trying to accomplish. Is there some fundamental reason why multiple import statements can’t be used for the same module?

I do not understand what you consider arcane there?

defmodule M do
  defmacro __using__(args) do
    quote generated: true do
      import __MODULE__, unquote(args)

  defdelegate …

PS: I do not know much about @pragdaves approach, except that I don’t use it, and also I’m not sure if I like the additional reductions it does cost.

I tried playing with this a bit and it caused my compile to blow up. This seems like a rathole, so I think I’ll punt for the moment…

Yes, sorry, typed that on my mobile. This works for me:

iex(1)> defmodule M do
...(1)>   defmacro __using__(args) do
...(1)>     quote generated: true do
...(1)>       import unquote(__MODULE__), unquote(args)
...(1)>     end
...(1)>   end
...(1)>   defdelegate ii(a), to: IO, as: :inspect
...(1)> end
iex(2)> defmodule M2 do
...(2)>   use M, only: [ii: 1]
...(2)>   def f(x), do: ii(x)
...(2)> end
iex(3)> M2.f 4

Thanks; that works nicely. I (carefully!) modified my code base to use it and it passes both dialyzer and my tests.

I’m not sure if I like the additional reductions it does cost.

I doubt whether the added reductions have any significant impact, unless a function is being called in the middle of a tight loop. Given that these functions are part of a library or component, this isn’t all that likely.

FWIW, I’m very happy with PragDave’s approach. It lets me organize my implementation code into a hidden set of small, cohesive modules. It also lets me write public (and thus testable and documentable) functions that aren’t part of the “official” API.

You can also disable warnings on the callers side:

1 Like

Cool! I like this better than the use approach, for multiple reasons:

  • It gets rid of a macro in my code base.
  • I can use import for all of my libraries.
  • The caller controls the module usage.
  • The whole thing seems more explicit.