[erlang-questions] Improve $handle_undefined_function

Loïc Hoguin essen@REDACTED
Tue Jan 22 12:10:56 CET 2013


On 01/22/2013 07:34 AM, Björn Gustavsson wrote:
> On Mon, Jan 21, 2013 at 7:04 PM, Loïc Hoguin <essen@REDACTED
> <mailto:essen@REDACTED>> wrote:
>
>     My proposal is to use an attribute to list the functions defined
>     like this, so that error_handler can make the distinction between
>     undef and badarg directly. The above function would then be written
>     like this:
>
>     $handle_undefined_function(__monitor, [process, Pid]) ->
>          do_something();
>     $handle_undefined_function(__demonitor, Pid) ->
>          do_something_else().
>
>
> I don't really get the rationale for your proposal.
>
> If have understood you correctly, I must know beforehand
> which functions that $handle_undefined_function would handle,
> and put all functions to be handled in an attribute.

Of course you need to know what functions exist in advance. How else do 
you expect to be able to call them in a reliable manner?

> 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.

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



More information about the erlang-questions mailing list