[erlang-questions] When to return {ok, Value} or just the Value

Gianfranco Alongi <>
Tue Nov 15 23:02:00 CET 2011


Maybe this is an over-specific example, but I have (lately) seen some
examples where people use predicates to check the sanity of input,
predicates that return 'true' or throw something.
I do very much not agree with this style as the predicates can fail as
part of the normal program flow and thus, using a sequential style or
predicate checking such that you check the next and next and next
until one fails, would do the trick and make the predicates
functionally clean.

If you expect something to "fail" <--- note the "" as part of the
normal flow, then return {error,term()}, else {ok,term()} or just
term(),

So, to answer your  question,

I think

try
   predicate,
   predicate,
   predicate,
   code
catch
  _ ->
end

could be written like (just taking an arity out of the blue)

case do_checks([ fun predicate/1,
                           fun predicate/1,
                           fun predicate/1]) of
     ok -> code
     {error,term()} ->
end,

instead.

/G




On Tue, Nov 15, 2011 at 1:35 AM, Steve Davis
<> wrote:
> Interesting post, Gianfranco.
> What would be a simple example of using such an approach for a
> function coded up in Erlang?
> Regs,Steve
> On Nov 14, 1:14 pm, Gianfranco Alongi <>
> wrote:> For those who find themselves writing case-ladders, look into
> monads> from haskell and draw upon the power of the sequencer idea.
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list