Research Article

Behaviour Preservation across Code Versions in Erlang

Algorithm 7

hello_server.erl.
(1) -module(hello_server).
(2)
(3) -behavior(gen_server).
(4)
(5) -record(state, count).
(6)
(7)
(8) gen_server Function Exports
(9)
(10)
(11) -export([     % The behavior callbacks
(12)  init/1,     % - initializes our process
(13)  handle_call/3, % - handles synchronous calls
(14)  handle_cast/2, % - handles asynchronous calls
(15)  terminate/2]). % - is called on shut-down
(16)
(17) 
(18)  gen_server Function Definitions
(19) 
(20)
(21) init([]) ->
(22)    ok, #statecount=.
(23)
(24)  -spec handle_call(get_count, any(),state, integer()) ->
(25)    reply, integer(),state, integer().
(26) handle_call(get_count, _From, #statecount=Count) ->
(27)    reply, Count, #statecount=Count+ .
(28)
(29)  -spec handle_cast(stop say_hello, state, integer()) ->
(30)    stop, any(),state, integer()
(31)    noreply, state, integer().
(32) handle_cast(stop, State) ->
(33)   stop, normal, State;
(34)
(35) handle_cast(say_hello, State) ->
(36)   io:format("Hello~n"),
(37)   noreply,
(38)    % #statecount = State#state.count+  % RIGHT
(39)    #statecount = State#state.count-   % WRONG
(40)   .
(41)
(42)  terminate(_Reason, _State) ->
(43)   error_logger:info_msg("terminating~n"),
(44)   ok.