title: Dynamically load/unload modules in Elixir, something like a plugin system
body: <p>I want to create something like a plugin system: a bunch of modules which get loaded/unloaded dynamically, add I insert/remove them.</p>
  
  <p>Something like this:</p>
  
  <pre><code># plugin1.eex
  defmodule MyApp.Plugins.Plugin1 do
  
    def plugin_main_func(arg1, arg2) do
      # some stuff
    end
  end
  
  # plugin2.eex
  defmodule MyApp.Plugins.Plugin2 do
  
    def plugin_main_func(arg1, arg2) do
      # some stuff 2
    end
  end
  
  
  # plugin3.eex
  defmodule MyApp.Plugins.Plugin3 do
  
    def plugin_main_func(arg1, arg2) do
      # some stuff 3
    end
  end
  </code></pre>
  
  <p>They'll have the same common function - "plugin_main_func" - which implementation will be different for each plugin.
  And somewhere in my app I'll have this:</p>
  
  <pre><code>plugins = load_plugins()
  Enum.each plugins, &amp;(&amp;1.plugin_main_func(1, "fdafdsfds"))
  </code></pre>
  
  <p>I then be able to add or remove plugins by creating or removing appropriate modules or files. <strong>Without</strong> having to hard-code/add/remove their names somewhere in an application as a list of strings <strong>as well</strong>.</p>
  
  <p>How can I achive this?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Given that a plugin must live in MyApp.Plugins and have a function  <code>plugin_main_func</code>, how can I observe a list of plugins they are currently present? I mere want to avoid hard-coding their names somewhere and load or run them only having these 2 conditions which are sufficient for finding all the plugins.</p>
  
  <p>When I find all of them, dynamically or semi-dynamically, I want to somehow be able to call <code>plugin_main_func</code> in each plugin. Without knowing the number of plugins and their exact names. How?</p>
  
  <p>I'm fine with loading them before compilation, not exactly at runtime.</p>