using guards in gen_server

Zvi <>
Sun Aug 29 11:20:56 CEST 2010


Hi,

When writing gen_server, do you put validation guards in API functions
or in handle_call / cast or in both ? What's the convention?

For example:

send(Pid) when is_pid(Pid) ->
   gen_server:call(?SERVER, {send, Pid}).

handle_call({send, Pid}, _From, _State) ->
   Resp = do_send(Pid),
   {reply,  Resp, State}.

OR:

send(Pid) ->
   gen_server:call(?SERVER, {send, Pid}).

handle_call({send, Pid}, _From, _State) when is_pid(Pid) ->
   Resp = do_send(Pid),
   {reply,  Resp, State}.

OR:

send(Pid) when is_pid(Pid) ->
   gen_server:call(?SERVER, {send, Pid}).

handle_call({send, Pid}, _From, _State) when is_pid(Pid) ->
   Resp = do_send(Pid),
   {reply,  Resp, State}.


More information about the erlang-questions mailing list