title: Hot code replacement in erlang
body: <p>I am working on my first real project in erlang, however, this code is simplified for brevity.  I want to be able to load a newer version of a file into my project <strong>remotely</strong> while it's running.  I've read about using a behavior like <code>gen_server</code> or <code>gen_fsm</code> which has this for free.  While that might achieve the result, I want to use this to learn how to do it, not just get it done.  I've read the docs about <a href="http://www.erlang.org/doc/reference_manual/code_loading.html#id83918" rel="noreferrer">code replacement</a>, and LYSE's bit about <a href="http://learnyousomeerlang.com/designing-a-concurrent-application#hot-code-loving" rel="noreferrer">Hot Code Loving</a>, among other things, but I haven't been able to find anything that works for what I'm doing, so here is the basic idea.</p>
  
  <pre><code>-module(reloading).
  
  -export([loop/0]).
  
  loop() -&gt;
      receive
          upgrade -&gt;
              ?MODULE:loop();
          hello -&gt;
              io:format("This is a test~n"),
              loop();
          _ -&gt;
              loop()
      end.
  </code></pre>
  
  <p>I am simply looping with the idea that I can send the message <code>upgrade</code> and it will load a newer version of the code.</p>
  
  <pre><code>$ erl
  Erlang R15B01 (erts-5.9.1) [source] [64-bit] [async-threads:0] [hipe] [kernel-poll:false]
  
  Eshell V5.9.1  (abort with ^G)
  1&gt; c(reloading).
  {ok,reloading}
  2&gt; Loop = spawn(reloading, loop, []).
  &lt;0.39.0&gt;
  3&gt; Loop ! hello.
  This is a test
  hello
  </code></pre>
  
  <p>At this point I change 10 line to <code>io:format("I have changed this!~n"),</code></p>
  
  <pre><code>4&gt; Loop ! upgrade.
  upgrade
  5&gt; Loop ! hello.  
  This is a test
  hello
  </code></pre>
  
  <p>I am expecting this <code>hello</code> call to print <code>I have changed this!</code> not <code>This is a test</code>.  I know I can simply call <code>c(reloading).</code> and have this work the way expected, but I'm looking to send the actual project a message rather than manually updating the code.  So where is my disconnect?  What am I doing wrong, that I should be doing in order to hot load this code?  As mentioned before, I am looking for a non-OTP solution for the sake of education.</p>