What's the difference between `use` and `#before_compile`?
What's the difference between use and #before_compile? Both can be used to inject some code in to current module.
One main difference is that the code in __using__ is called and return value injected as soon as you call use within a module, while __before_compile__ is called and the return value injected after all the code directly inside the module is executed, just before the module is compiled. A simple program to demonstrate the difference: defmodule When do defmacro __using__(_) do IO.inspect :using __CALLER__.module |> Module.definitions_in |> IO.inspect quote do end end defmacro __before_compile__(_) do IO.inspect :before_compile __CALLER__.module |> Module.definitions_in |> IO.inspect quote do end end end defmodule Main do #before_compile When use When IO.puts "added `#before_compile` and `use`" def add(x, y), do: x + y end Output: :using  added `#before_compile` and `use` :before_compile [add: 2] ExUnit uses #before_compile to define a function that returns all the tests to make it possible for the ExUnit runner to get and run all the tests.
What is the correct syntax in metaprogramming assignment in Julia?
How can I call .WHY on a subroutine in a class in Perl 6?
Using Roslyn, if I have an IdentifierNameSyntax, can I find the member type it refers to (field, property, method…)
Elixir - Define functions in module by metaprogramming
Disambiguate the various quoting mechanisms in Julia metaprogramming
How to initialize list using macros and then generate same set of functions in 2 different modules?
Type-safe metaprogramming? Using strings to query ceylon.language for meta information?
Interpolating an Expression into an Expression
Loop unrolling? in Julia with metaprogramming
Macro for a function template: escaping issues
Macro that defines macro returning struct constant
Array of Expressions to an Array Definition
Code generation from restricted set of input
System of equations using metaprogramming
How to define a partial copy constructor metaprogramically in Julia?
How to interpolate into a Julia “for” expression?