[erlang-questions] Elixir Protocols in Erlang & a Strange Warning
José Valim
jose.valim@REDACTED
Fri Dec 13 09:27:53 CET 2013
I have seen this question asked earlier in Stack Overflow and answered it
there:
http://stackoverflow.com/questions/20556472/elixir-protocols-in-erlang-a-strange-warning/20561973#20561973
I have also reported a bug to erlang-bugs this time.
One final note: although parameterized modules were removed from R16, the
underlying dispatch mechanism, which is used in the descried issue, is not
deprecated and it won't be removed.
*José Valim*
www.plataformatec.com.br
Skype: jv.ptec
Founder and Lead Developer
On Fri, Dec 13, 2013 at 8:10 AM, Dmitry Kolesnikov
<dmkolesnikov@REDACTED>wrote:
> Hello,
>
> Essentially you are trying to use parametrized modules, which get
> deprecated in R14/15 and dropped from R16.
> See for details:
> * http://www.erlang.org/news/35
> * http://www.erlang.se/workshop/2003/paper/p29-carlsson.pdf
>
> This type of problems are solvable either via pattern match or currying:
>
> -module(stringer).
> -export([stringer/1,sample/0]).
>
> stringer(V) when is_list(V) ->
> fun() -> to_string(V, nop) end;
> stringer(V) when is_atom(V) ->
> fun() -> to_string(V, nop) end;
> stringer(_V) ->
> fun() -> not_implemented end.
>
> to_string(V, _Nop) ->
> Buffer = io_lib:format("~p",[V]),
> lists:flatten(Buffer).
>
> sample() ->
> io:format("~p~n", [(stringer([1,2]))()]),
> io:format("~p~n", [(stringer(cute_atom))()]),
> io:format("~p~n", [(stringer(13))()]).
>
>
> Best Regards,
> Dmitry
>
> On Dec 13, 2013, at 8:31 AM, Kaveh Shahbazian <kaveh.shahbazian@REDACTED>
> wrote:
>
> I wanted to write something like ((IStringer)object).ToString() (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:
>
> ?stringer(my_val):to_string().
>
> And it either returns the expected value or not_implemented atom!
>
> But 2 questions:
>
> 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.).
>
> 2 - Why I get this warning? That call actually never fails.
>
> The warning:
>
> 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
>
> The code:
>
> -module(stringer).
> -export([to_string/1,sample/0]).
>
> -define(stringer(V), {stringer, V}).
>
> to_string({stringer, V}) when is_list(V) ->
> to_string(V, nop);
> to_string({stringer, V}) when is_atom(V) ->
> to_string(V, nop);
> to_string({stringer, _V}) ->
> not_implemented.
>
> to_string(V, _Nop) ->
> Buffer = io_lib:format("~p",[V]),
> lists:flatten(Buffer).
>
> sample() ->
> 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()]).
>
> And the output is:
>
> "[1,2]"
> "cute_atom"
> not_implemented
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131213/2b03ee4b/attachment.htm>
More information about the erlang-questions
mailing list