Wiger Ulf ulf.wiger@REDACTED
Tue Jul 29 12:18:09 CEST 2003

From: "Fredrik Linder" <fredrik.linder@REDACTED>

> One of the uses I have for behaviours is to maintain knowledge
> about why a certain function is exported from a module.
> As an example I'd have:
>  o One behaviour to list operational functions (start/stop)
>  o One behaviour to list administrative functions (set cache size/...)
>  o One behaviour for each other functionality (add user/remove user or
> permit_user/is_user_permitted or determine user violations (whatever))
> In this way I'd say that multiple behaviours are very desireable.
> This could of course be put into separate modules, but doing so
> the knowledge that I'd like to keep very close together, and is hence not
> desireable.
> It'd only make the code more difficult to read and understand.

As far as I can tell, this discussion is analogous to the single- vs
multiple inheritance debate in OO.

The main thing that is missing in order to support use of multiple
behaviours in one module is some remapping facility. One could
imagine using funs for this in a way that could be transparent and
add minimal overhead. Example:

-behaviour(a).    % expects init/1 and foo/0
-behaviour(b).    % expects init/1, foo/0 and bar/0

-export([a_init/1, a_foo/0]).
-export([b_init/1, b_foo/0, b_bar/0]).


behaviour_redirect(a) ->
   [{init, 1, fun a_init/1},
    {foo, 0, fun a_foo/0}];
behaviour_redirect(b) ->
    [{init, 1, fun b_init/1},
     {foo, 0, fun b_foo/0},
     {bar, 0, fun b_bar/0}].

%% Now we can separate overlapping callbacks and group
%% the functions in the most intuitive fashion.
a_init(Arg) -> ...
a_foo() -> ...

b_init(Arg) -> ...
b_foo() -> ...
b_bar() -> ...

The behaviour would have to understand to use
function_exported(M,behaviour_redirect,1) and use default callbacks
for functions that are not explicitly redirected. This operation would
be called when the module is "instantiated". It would entail some overhead
for behaviours that are not really instantiated (such as mnesia callbacks),
but behaviour redirection would not have to be mandatory.


More information about the erlang-questions mailing list