A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://www.erlang.org/doc/system/modules below:

Modules — Erlang System Documentation v28.0.1

Module Syntax

Erlang code is divided into modules. A module consists of a sequence of attributes and function declarations, each terminated by a period (.).

Example:

-module(m).          % module attribute
-export([fact/1]).   % module attribute

fact(N) when N>0 ->  % beginning of function declaration
    N * fact(N-1);   %  |
fact(0) ->           %  |
    1.               % end of function declaration

For a description of function declarations, see Function Declaration Syntax.

Module Attributes

A module attribute defines a certain property of a module.

A module attribute consists of a tag and a value:

-Tag(Value).

Tag must be an atom, while Value must be a literal term. As a convenience in user-defined attributes, if the literal term Value has the syntax Name/Arity (where Name is an atom and Arity a positive integer), the term Name/Arity is translated to {Name,Arity}.

Any module attribute can be specified. The attributes are stored in the compiled code and can be retrieved by calling Module:module_info(attributes), or by using the module beam_lib in STDLIB.

Several module attributes have predefined meanings. Some of them have arity two, but user-defined module attributes must have arity one.

Pre-Defined Module Attributes

Pre-defined module attributes is to be placed before any function declaration.

Behaviour Module Attribute

It is possible to specify that the module is the callback module for a behaviour:

-behaviour(Behaviour).

The atom Behaviour gives the name of the behaviour, which can be a user-defined behaviour or one of the following OTP standard behaviours:

The spelling behavior is also accepted.

The callback functions of the module can be specified either directly by the exported function behaviour_info/1:

behaviour_info(callbacks) -> Callbacks.

or by a -callback attribute for each callback function:

-callback Name(Arguments) -> Result.

Here, Arguments is a list of zero or more arguments. The -callback attribute is to be preferred since the extra type information can be used by tools to produce documentation or find discrepancies.

Read more about behaviours and callback modules in OTP Design Principles.

Record Definitions

The same syntax as for module attributes is used for record definitions:

-record(Record, Fields).

Record definitions are allowed anywhere in a module, also among the function declarations. Read more in Records.

Preprocessor

The same syntax as for module attributes is used by the preprocessor, which supports file inclusion, macros, and conditional compilation:

-include("SomeFile.hrl").
-define(Macro, Replacement).

Read more in Preprocessor.

Setting File and Line

The same syntax as for module attributes is used for changing the pre-defined macros ?FILE and ?LINE:

-file(File, Line).

This attribute is used by tools, such as Yecc, to inform the compiler that the source program is generated by another tool. It also indicates the correspondence of source files to lines of the original user-written file, from which the source program is produced.

Types and function specifications

A similar syntax as for module attributes is used for specifying types and function specifications:

-type my_type() :: atom() | integer().
-spec my_function(integer()) -> integer().

Read more in Types and Function specifications.

The description is based on EEP8 - Types and function specifications, which is not to be further updated.

Documentation attributes

The module attribute -doc(Documentation) is used to provide user documentation for a function/type/callback:

-doc("Example documentation").
example() -> ok.

The attribute should be placed just before the entity it documents.The parenthesis are optional around Documentation. The allowed values for Documentation are:

It is possible to have multiple Metadata doc attributes per entity, but only a single documentation string entry is allowed.

See the Documentation guide in the Erlang Reference Manual for more details.

The feature directive

While not a module attribute, but rather a directive (since it might affect syntax), there is the -feature(..) directive used for enabling and disabling features.

The syntax is similar to that of an attribute, but has two arguments:

-feature(FeatureName, enable | disable).

Note that the feature directive can only appear in a prefix of the module.

Comments can be placed anywhere in a module except within strings and quoted atoms. A comment begins with the character %, and continues up to but not including the next end of line. A comment has no effect, being essentially equivalent to white space.

module_info/0 and module_info/1 functions

The compiler automatically inserts the two special, exported functions into each module:

When called, these functions retrieve information about the module.

module_info/0

The module_info/0 function in each module returns a list of {Key,Value} tuples with information about the module. At the time writing, the list contain tuples having the following Keys: module, attributes, compile, exports, and md5. The order and number of tuples may change without prior notice.

module_info/1

The call module_info(Key), where Key is an atom, returns a single piece of information about the module.

The following values are allowed for Key:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4