title: Elixir Protocols in Erlang & a Strange Warning
body: <p>I wanted to write something like <code>((IStringer)object).ToString()</code> (in C#) in Erlang. After some studying I've learnt that Elixir has something called Protocols that pretty much resembles the same thing of C# (in an inside-out manner). Then I came up with this idea/code in Erlang - which is nice enough to me like:</p>
  
  <pre><code>?stringer(my_val):to_string().
  </code></pre>
  
  <p>And it either returns the expected value or <code>not_implemented</code> atom!</p>
  
  <p>But 2 questions:</p>
  
  <p>1 - Why nobody use this or promote things based on stateful modules in Erlang? (OTP aside and from talking to some Erlangers they did not know that actually OTP is built around this! So really there is a need to change how Erlang is being taught and promoted. It's possible that I am confused.).</p>
  
  <p>2 - Why I get this warning? That call actually can never fails.</p>
  
  <p>The warning:</p>
  
  <pre><code>stringer.erl:18: Warning: invalid module and/or function name; this call will always fail
  stringer.erl:19: Warning: invalid module and/or function name; this call will always fail
  stringer.erl:20: Warning: invalid module and/or function name; this call will always fail
  </code></pre>
  
  <p>The code:</p>
  
  <pre><code>-module(stringer).
  -export([to_string/1,sample/0]).
  
  -define(stringer(V), {stringer, V}).
  
  to_string({stringer, V}) when is_list(V) -&gt;
      to_string(V, nop);
  to_string({stringer, V}) when is_atom(V) -&gt;
      to_string(V, nop);
  to_string({stringer, _V}) -&gt;
      not_implemented.
  
  to_string(V, _Nop) -&gt;
      Buffer = io_lib:format("~p",[V]),
      lists:flatten(Buffer).
  
  sample() -&gt;
      io:format("~p~n", [?stringer([1,2]):to_string()]),
      io:format("~p~n", [?stringer(cute_atom):to_string()]),
      io:format("~p~n", [?stringer(13):to_string()]).
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>"[1,2]"
  "cute_atom"
  not_implemented
  </code></pre>
  
  <p>I am doing this on Erlang R16 B2 (V5.10.3) 32 bit on Windows 8 64 bit.</p>