[erlang-questions] Behaviours in a module

Nuku Ameyibor nayibor@REDACTED
Tue Feb 17 17:18:00 CET 2015


Thanks for the clarifications Van .

From: Vance Shipley
Sent: ‎17/‎02/‎2015 04:30
To: Nuku Ameyibor
Cc: erlang questions
Subject: Re: [erlang-questions] Behaviours in a module
I don't see any examples of multiple behaviours in OTP today however I
do remember there being a few cases at one time.
Basically if there is no conflict in callback name/arity

(*)  grep --recursive --count --files-with-matches '^-behaviour('
otp_src_17.3 | grep '[2-9]$'

On Tue, Feb 17, 2015 at 9:10 AM, Nuku Ameyibor <nayibor@REDACTED> wrote:
> How many application behaviors can you attach to a single module and how
> does this affect  the behavior of the module .

If there is no conflict in callbacks' name/arity you can include
multiple behaviours.
I don't see any examples of multiple behaviours in OTP today however I
do remember there being a few cases at one time where application and
supervisor behaviours appeared in one module.

> can you put different behaviors into a single module and evoke them and make
> that one module have all the behaviours eg. can a single module be a
> gen_server,supervisor  or are there some special instances where this is
> allowed .(genserver and supervisor both have one init function so i was
> beginning to doubt this but what if your behaviors have different names):

Since gen_server and supervisor both call init/1 you have a potential conflict.
You could have the different behaviours handled in separate clauses, based
on the argument, thereby avoiding a conflict. This would really be an exercise
in obfuscation however.  Don't do it.

> i.e. create your own custom behaviours

Custom behaviours have been supported for quite some time.  Basically
all a behaviour is is a design pattern for modules you write.  By
using the -behaviour attribute in your (callback) module you are
declaring it to match the pattern.  During compilation erl_lint checks
to see that your callback module exports the required functions.  You
may create your own behaviours by including a -callback attribute in
the module which implements the pattern (main loop), the name of that
module isis used in the -behaviour attribute of your callback module.

     http://www.erlang.org/doc/design_principles/spec_proc.html#behaviours


-- 
     -Vance



More information about the erlang-questions mailing list