From 146260727638e8a477aeda7828364ce45dc506a0 Mon Sep 17 00:00:00 2001 From: Hans Bolinder Date: Tue, 15 Apr 2014 09:38:41 +0200 Subject: Introduce the attribute -optional_callbacks in the context of behaviours --- system/doc/design_principles/spec_proc.xml | 76 ++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 20 deletions(-) (limited to 'system/doc') diff --git a/system/doc/design_principles/spec_proc.xml b/system/doc/design_principles/spec_proc.xml index e4fb5fdca7..e849388a38 100644 --- a/system/doc/design_principles/spec_proc.xml +++ b/system/doc/design_principles/spec_proc.xml @@ -431,43 +431,79 @@ loop(...) ->
User-Defined Behaviours -

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, add -callback attributes in the - behaviour module to describe the expected callbacks:

+ +

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, add + -callback attributes in the behaviour module to describe + the expected callback functions:

+ -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 attribute is - supported by -callback attribute.

-

Alternatively you may directly implement and export the function:

+ +

where each Name is the name of a callback function and + Arg and Res are types as described in + Specifications for functions in Types and Function + Specifications. The whole syntax of the + -spec attribute is supported by -callback + attribute.

+

Callback functions that are optional for the user of the + behaviour to implement are specified by use of the + -optional_callbacks attribute:

+ + +-optional_callbacks([OptName1/OptArity1, ..., OptNameK/OptArityK]). + +

where each OptName/OptArity specifies the name and arity + of a callback function. Note that the -optional_callbacks + attribute is to be used together with the -callback + attribute; it cannot be combined with the + behaviour_info() function described below.

+

Tools that need to know about optional callback functions can + call Behaviour:behaviour_info(optional_callbacks) to get + a list of all optional callback functions.

+ +

We recommend using the -callback attribute rather + than the behaviour_info() function. The reason is that + the extra type information can be used by tools to produce + documentation or find discrepancies.

+ +

As an alternative to the -callback and + -optional_callbacks attributes you may directly implement + and export behaviour_info():

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

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.

+ +

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([start_link/2, init/3, ...]). -callback init(State :: term()) -> 'ok'. -callback handle_req(Req :: term(), State :: term()) -> {'ok', Reply :: term()}. -callback terminate() -> 'ok'. +-callback format_state(State :: term()) -> term(). + +-optional_callbacks([format_state/1]). %% Alternatively you may define: %% -- cgit v1.2.3