Appendix B. OTP Templates

These are the full templates for gen_server, supervisor, and application from the Emacs mode for Erlang. Some pieces are more useful than others, but seeing the full set of expected responses can be useful. (In this context, a template is just a file full of code you can use as a base for creating your own code.)

Note

Remember, the noreply atom doesn’t mean “there will never be a reply” but rather that “this response isn’t a reply.”

Example B-1. A gen_server template from the Erlang mode for Emacs
%%%-------------------------------------------------------------------
%%% @author $author
%%% @copyright (C) $year, $company
%%% @doc
%%%
%%% @end
%%% Created : $fulldate
%%%-------------------------------------------------------------------
-module($basename).

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
         handle_call/3,
         handle_cast/2,
         handle_info/2,
         terminate/2,
         code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {}).

%%%===================================================================
%%% API
%%%===================================================================

%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
%% @end
%%--------------------------------------------------------------------
start_link() ->
        gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

%%%=================================================================== ...

Get Introducing Erlang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.