Elegant solution for constants?

If we talk about a simplest examples then even 10_000 generated functions does not give a warning mentioned above. :+1:

Consider below example:

defmodule Example do
  defmacro __using__(_opts \\ []) do
    quote unquote: false do
      for item <- Enum.to_list(1..100) do
        def unquote(:"sample#{item}")(), do: {:ok, unquote(item)}
      end
    end
  end
end

With this we can generate a new app:

$ mix new example
# above example goes here
$ youreditorbin lib/example.ex
$ iex
# …
iex> Enum.each(1..100, fn index ->
...>  File.write! "test#{index}.ex", "defmodule Test#{index} do\n  use Example\nend\n"
...> end)

Now let’s try to measure how long it would take to compile such a project:

$ cd ..
$ time mix compile --force

real    0m0,622s
user    0m5,268s
sys     0m0,999s

1s - is it a lot? For a simple example it’s definitely a lot, but for 10_000 generated functions grouped in 100 modules? :thinking:

I believe that some answers on forum may be outdated or their context describes more specific cases. There were many improvements in Elixir language especially around compiling large projects. On a modern PC with latest Erlang and Elixir versions even with so crazy example it take just a second. For sure you definitely don’t want to have thousands of “just 1s”, but I never generated so many functions in any project and if your project becomes really, really big then said 1s could not be even noticeable. :see_no_evil: