elixir-lang/elixir — pull request #1677 by yrashk (2013-09-01 19:24:30+00:00)
Title: Initial implementation of compiler-wide module callbacks support

This is a conversation starter. I want to suggest a feature that allows specifying "global" module callbacks. To my best recollection, we already discussed this idea in general and were generally favouring it.

Included patch is a proof of concept (i.e., no tests) that allows following to happen:

```elixir
defmodule Bcac.Mixfile do
use Mix.Project

defmodule Callbacks do

defmacro __before_compile__(_) do
quote do
IO.puts "I'm starting module #{inspect __MODULE__}"
end
end

defmacro __after_compile__(_, _) do
quote do
IO.puts "I'm finishing module #{inspect __MODULE__}"
end
end

end

def project do
[ app: :bcac,
version: "0.0.1",
elixir: "~> 0.10.2-dev",
deps: deps,
elixirc_options: [before_compile: Callbacks, after_compile: Callbacks]
]
end

# Configuration for the OTP application
def application do
[]
end

# Returns the list of dependencies in the format:
# { :foobar, "~> 0.1", git: "https://github.com/elixir-lang/foobar.git" }
defp deps do
[]
end
end
```

When we run `mix`, it produces this:

```
$ mix
I'm starting module Bcac
I'm finishing module Bcac
Compiled lib/bcac.ex
Generated bcac.app
```

One of the main (current) motivations for this improvement for me is a desire to include "common stuff" (such as "use ExLogger") in one place without worrying about keeping the entire codebase updated.

As you can see, the implementation is quite trivial. Once we agree that this is where we want to go, I will add tests.

cc @ericmj