From 8ea0c7c566d2c356f273806c42615f31b9e946d3 Mon Sep 17 00:00:00 2001 From: Stavros Aronis Date: Tue, 10 May 2011 18:31:02 +0300 Subject: Update the documentation with information on the callback attribute --- system/doc/design_principles/spec_proc.xml | 48 ++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'system/doc') diff --git a/system/doc/design_principles/spec_proc.xml b/system/doc/design_principles/spec_proc.xml index f0f62891b6..a1c862e004 100644 --- a/system/doc/design_principles/spec_proc.xml +++ b/system/doc/design_principles/spec_proc.xml @@ -411,30 +411,48 @@ loop(...) ->

To implement a user-defined behaviour, write code similar to code for a special process but calling functions in a callback module for handling specific tasks.

-

If it is desired that the compiler should warn for missing - callback functions, as it does for the OTP behaviours, implement - and export the function:

+

If it is desired that the compiler should warn for missing callback + functions, as it does for the OTP behaviours, add callback attributes in the + behaviour module to describe the expected callbacks:

+ +-callback Name1(Arg1_1, Arg1_2, ..., Arg1_N1) -> Res1. +-callback Name2(Arg2_1, Arg2_2, ..., Arg2_N2) -> Res2. +... +-callback NameM(ArgM_1, ArgM_2, ..., ArgM_NM) -> ResM. +

where NameX are the names of the expected callbacks and + ArgX_Y, ResX are types as they are described in Specifications + for functions in Types and + Function Specifications. The whole syntax of spec attributes is + supported by callback attributes.

+

Alternatively you may directly implement and export the function:

behaviour_info(callbacks) -> [{Name1,Arity1},...,{NameN,ArityN}]. -

where each {Name,Arity} specifies the name and arity of - a callback function.

+

where each {Name,Arity} specifies the name and arity of a callback + function. This function is otherwise automatically generated by the compiler + using the callback attributes.

When the compiler encounters the module attribute - -behaviour(Behaviour). in a module Mod, it will call - Behaviour:behaviour_info(callbacks) and compare the result - with the set of functions actually exported from Mod, and - issue a warning if any callback function is missing.

+ -behaviour(Behaviour). in a module Mod, it will call + Behaviour:behaviour_info(callbacks) and compare the result with the + set of functions actually exported from Mod, and issue a warning if + any callback function is missing.

Example:

%% User-defined behaviour module -module(simple_server). -export([start_link/2,...]). --export([behaviour_info/1]). -behaviour_info(callbacks) -> - [{init,1}, - {handle_req,1}, - {terminate,0}]. +-callback init(State :: term()) -> 'ok'. +-callback handle_req(Req :: term(), State :: term()) -> {'ok', Reply :: term()}. +-callback terminate() -> 'ok'. + +%% Alternatively you may define: +%% +%% -export([behaviour_info/1]). +%% behaviour_info(callbacks) -> +%% [{init,1}, +%% {handle_req,2}, +%% {terminate,0}]. start_link(Name, Module) -> proc_lib:start_link(?MODULE, init, [self(), Name, Module]). @@ -452,7 +470,7 @@ init(Parent, Name, Module) -> -module(db). -behaviour(simple_server). --export([init/0, handle_req/1, terminate/0]). +-export([init/0, handle_req/2, terminate/0]). ... -- cgit v1.2.3