[erlang-questions] Suggestion for the Mnesia manual
Jouni Rynö
Jouni.Ryno@REDACTED
Mon May 5 12:44:06 CEST 2008
So from analysis point of view, recursion still would be better? (sorry,
deleted the original example, but this was directly from my own code,
revealed by dialyzer ...)
Instead of this (dialyzer tells me, that it produces value of type
['ok'])
[ok = gen_server:cast(Client, {parameter, Para}) || Client <-
ClientList]
use the recursion
serve_clients(Message, [Client|ClientList]) ->
ok = gen_server:cast(Client, Message),
serve_clients(Message, ClientList);
serve_clients(Message, []) ->
ok.
Or to keep list comprehensions and make dialyzer happy with
Oks = [ok || _Res <- ClienList],
Oks = [ok = gen_server:cast(Client, {parameter, Para}) || Client <-
ClientList]
Jouni
On Mon, 2008-05-05 at 09:48 +0300, Kostis Sagonas wrote:
> Philip Robinson wrote:
> >
> > I am a big fan of putting the expected return value on every line that
> > is otherwise used for side-effects. Unification will then ensure that
> > the code aborts on the first line in error; the later lines don't even
> > get a chance to run.
> >
> > i.e.:
> > init() ->
> > {atomic, ok} = mnesia:create_table(t1...),
> > {atomic, ok} = mnesia:create_table(t2...),
> > {atomic, ok} = mnesia:create_table(t2...).
> >
> > Even using anonymous vars helps, e.g.:
> > {ok, _} = some_side_effect_fun(),
> > ensures that the return value wasn't {error, 'some message'} before continuing.
>
> I am a very big fan of the practice that Philip advocates because it can
> avoid serious and often very subtle bugs in Erlang code. In fact, in
> the latest dialyzer, the one in R12B-2, we have added a command-line
> option -Wunmatched_returns that warns for calls used for side-effects
> which ignore their return value. (The option is off by default.)
>
> It strongly prefers code such as the above and will warn about calls
> which ignore their return value, such as:
>
> init() ->
> mnesia:create_table(t1, [{attributes,record_info(fields, foo)},
> {disk_only_copies, [node()]} ]),
> mnesia:create_table(t2, [{attributes,record_info(fields, bar)},
> {disk_only_copies, [node()]} ]),
> mnesia:create_table(t3, [{attributes,record_info(fields, baz)},
> {disk_only_copies, [node()]} ]).
>
> or for code which exists all over the place, even in standard libraries,
> such as the one below (from escript.erl):
>
> compile(Parse, Args) ->
> case compile:forms(Parse, [report]) of
> {ok,Module,BeamCode} ->
> erlang:load_module(Module, BeamCode),
> run_code(Module, Args);
> _Other ->
> fatal("There were compilation errors.")
> end.
>
> which makes implicit assumptions (e.g. that the load always succeeds)
> that might not always hold.
>
> Kostis
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
--
Jouni Rynö mailto://Jouni.Ryno@fmi.fi/
http://space.fmi.fi/~ryno/
Finnish Meteorological Institute http://www.fmi.fi/
P.O.BOX 503 Tel (+358)-9-19294656
FIN-00101 Helsinki FAX (+358)-9-19294603
Finland priv-GSM (+358)-50-5302903
"It's just zeros and ones, it cannot be hard"
More information about the erlang-questions
mailing list