[erlang-questions] Package Support/Use

Olivier Sambourg <>
Mon Nov 6 11:54:36 CET 2006


Hi,

You could also use an assertion macro (which was suggested to me a while
back on this list):

-define(match(ExpectedResult, Expr, Error),
    ExpectedResult = fun() ->
        Res = (catch (Expr)),
        case Res of
            ExpectedResult     -> Res;
            _               -> throw({match_error, Error})
        end
    end()).

And use it like this:

?match({ok,FD}, open(Name), open_file_error),
?match({ok,Data}, read(FD), read_data_error),
etc.

Keeps the TradErl philosophy, but with better error reporting. And you don't
need to figure out every possible error in either open() or read() (but you
still can if you want or if you have to : the exception will be thrown
before the failed assertion)


Olivier


On 11/6/06, Mats Cronqvist <> wrote:
>
> Christian S wrote:
> > On 11/6/06, Bob Ippolito <> wrote:
> >> On 11/6/06, Mats Cronqvist <> wrote:
> >> > Richard A. O'Keefe wrote:
> >> > > Dominic Williams <> wrote:
> >> > >       - returning tagged values vs. throwing
> >> >    i hope you misremember, cause returning tagged tuples is truly
> >> abominable.
> >> > only a closet C programmer would use that idiom.
> >
> > How are tagged tuples bad? How do one reason for such an opinion?
>
>   as a really dumb example, consider three functions open(Filename),
> read(FileDescriptor), and process_data(Data) that can either fail or
> return
> useful data. in C you'd do something like this;
>
>   if ( ( fd = open(name)) == NULL ) return -1;
>   if ( read(fd,&data) == -1 ) return -2;
>   if ( process_data(&data,&pd) == -1 ) return -3;
>   return 0;
>
>   in Traditional Erlang something like this;
>
>   {ok,FD} = open(Name),
>   {ok,Data} = read(FD),
>   {ok,PD} = process_data(Data),
>
>   in BettErl (tm);
>
>   process_data(read(open(Name))),
>
>   TradErl is more verbose and binds worthless intermediate variables.
>
>   TradErl has poor error reporting.
>   it will either succeed or fail with "badmatch". not very helpful. the
> BettErl
> function will either succeed or throw useful stuff like
> {couldnt_open,Name} or
> {read_failure,enoent}, because that's how functions are written in
> BettErl; they
> either returns proper data or throws something meaningful.
>
>   so, to answer your question, tagged tuples encourages verbose code with
> poor
> error reporting.
>   in short, bad style. IMO.
>
>   mats
>
> p.s. i'm aware there are some oopses in the meta-code (e.g. memory leaks).
> but i
> don't think that has any bearing on my conclusion.
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20061106/1c51eda7/attachment.html>


More information about the erlang-questions mailing list