no more tagged returns
Vance Shipley
vances@REDACTED
Mon Feb 21 21:31:09 CET 2005
Today I discovered erlang:error/2 (apparently added in R10) which
completes what is needed to write code which behaves in the way it
really should when called with the wrong arguments. Nice.
I like to write all API functions with as many clause guards as
possible(*) to make most effect use of the error reports. Now I
can write the clause guards in the server where the procedures are
implemented.
-Vance
(*) The "fail fast" principle.
-module(new_api).
-behaviour(gen_server).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-export([foo_to_list/2]).
foo_to_list(S, Arg) ->
case gen_server:call(S, Arg) of
{error, Reason} ->
erlang:error(Reason, [S, Arg]);
Result ->
Result
end.
init(_) ->
{ok, []}.
handle_cast(_Event, State) ->
{noreply, State}.
handle_call(foo, _From, State) ->
{reply, atom_to_list(foo), State};
handle_call(Atom, _From, State) when is_atom(Atom) ->
{reply, {error, function_clause}, State};
handle_call(_Event, _From, State) ->
{reply, {error, bardarg}, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
More information about the erlang-questions
mailing list