[erlang-questions] Improve $handle_undefined_function

Vlad Dumitrescu <>
Tue Jan 22 12:57:47 CET 2013


Hi,

On Tue, Jan 22, 2013 at 12:10 PM, Loïc Hoguin <> wrote:

> On 01/22/2013 07:34 AM, Björn Gustavsson wrote:
>
>> Why not use the existing -export() attribute and write like
>>
> this:
>>
>> -export([monitor/2,demonitor/**1]).
>>
>> monitor(process, Pid) ->
>>     do_something.
>>
>> demonitor(Pid) ->
>>     do_something_else().
>>
>> So what would be the advantage of your version?
>>
>
> It still allows Evan Miller to write this:
>
> -attr([first_name/1, last_name/1, ...]).
>
> $handle_undefined_function(F, [Model]) ->
>     {_, Value} = lists:keyfind(F, 1, Model),
>     Value.
>
>
I'm sorry, but I don't think you are answering Björn's question. For this
particular case, why not write regular Erlang like

first_name(Model) -> get(first_name, Model).
last_name(Model) -> get(last_name, Model).
get(F, [Model]) ->
    {_, Value} = lists:keyfind(F, 1, Model),
    Value.

which is much clearer to understand. One argument might be that there is
some duplication, but I think that it could be handled with much more
elegance if we had abstract patterns implemented.

Do you have a more relevant example where using $handle_undefined_function
gives significant advantage?

best regards,
Vlad

 As you can see he doesn't have to worry about adding clauses to handle
> undef, which is good.
>
> This also still allows you to do -extends:
>
> %% Functions you override.
> -export([ponies/1]).
>
> %% Functions you don't.
> -attr([horses/1, cows/1]).
>
> $handle_undefined_function(F, Args) ->
>     apply(base_module, F, Args).
>
> Again you still don't have to handle undef.
>
> Tools can expect these functions. If you allow specs for undefined
> functions then Dialyzer can also act as if they were defined normally.
>
> Implementing it like this, without pushing the burden of handling undef to
> the user, would avoid making half the community scream in terror and
> endlessly ask for the removal of the feature (like with pmods).
>
>
> --
> Loïc Hoguin
> Erlang Cowboy
> Nine Nines
> http://ninenines.eu
> ______________________________**_________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/**listinfo/erlang-questions<http://erlang.org/mailman/listinfo/erlang-questions>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130122/3262f955/attachment.html>


More information about the erlang-questions mailing list