Erlang/OTP Forums

Author Message

<  Erlang questions mailing list  ~  global_name_server question.

klacke at bluetail.com
Posted: Fri Jun 04, 1999 9:40 pm Reply with quote
Guest
Craig Dickson" <crd_at_inversenet.com> wrote:
>
> Peter Olin wrote:
>
> > Not only is this awkward, but also, instead of simply handling the values
> > we're interested in we need to match structures and explicitly force our
> > code to crash where it would be natural to have that behaviour implicitly.
>
> True. It isn't quite true, though, that nested function calls are
> impossible.
> There is another way to write it than your copy2:
>
> copy3(File1, File2) ->
> file:write_file("dot_login",
> begin {ok, B} = file:read_file(".login"), B end).


I think Peter has a point here, on the other hand if we had the
situation that all the library functions were to exit/1 on failure
I'm sure that Peter would be the first to tell us that this
idea of always calling exit/1 isn't that good either.

The question of whether to return {error, Reason} or to
call exit/1 simply isn't possible to answer.

Today the erlang libraries are almost consistent in
error return values with the {error, Reason} tuple.

The programmer can simulate an exit/1 behaviour by
adding match assertions in the code as in ok = io:format...
I do that often.

Nesting funtion calls isn't that easy though and I didn't like
the neither the proposed begin end thinge nor the Assert fun

This discussion is 10 years old, we've had this debate
for years and we've never even come close to consencus .

Perhaps we should have yet another API as in:

Fd = file:x_open(File, [read, raw]),
B = file:x_read(Fd, 2048),


and

io:x_format("Hello", []),

mnesia:x_transaction(Fun),


Which fails with exit/1 on error.

??


/klacke


Post generated using Mail2Forum (http://m2f.sourceforge.net)
patrickdlogan at home.com
Posted: Sat Jun 05, 1999 12:39 am Reply with quote
Guest
Claes Wikstrom writes:

> Perhaps we should have yet another API as in:
>
> Fd = file:x_open(File, [read, raw]),
> B = file:x_read(Fd, 2048),
>
> and
>
> io:x_format("Hello", []),
>
> mnesia:x_transaction(Fun),

Yes, this is along the lines of what I was thinking of as a solution
in a future release. Establish some convention that there are
functions named y and functions named y' (y prime) to indicate error
return and exit/1 respectively. The convention becomes easy to
remember, there is choice of both mechanisms, and there is backward
compatibility with incremental migration to the new functions as
desired.

The only thing I would change from klacke's suggestion is to put the
mnemonic at the end of the function name rather than the
beginning. Then lexical sorting places the companion functions side by
side. Of course it could be argued that the mnemonic at the beginning
puts the new functions lexically together, but that effect can be
achieved just as easily via a pattern match (e.g. grep).

--
Patrick D. Logan mailto:patrickdlogan_at_home.com


Post generated using Mail2Forum (http://m2f.sourceforge.net)
ulf.wiger at etxb.ericsso
Posted: Sun Jun 06, 1999 1:50 pm Reply with quote
Guest
On Fri, 4 Jun 1999, Claes Wikstrom wrote:

klacke>> There is another way to write it than your copy2:
klacke>>
klacke>> copy3(File1, File2) ->
klacke>> file:write_file("dot_login",
klacke>> begin {ok, B} = file:read_file(".login"), B end).
klacke>

Personally, I much prefer to write a wrapper function:

copy4(File1, File2) ->
file:write_file(read_file(File1), File2).

read_file(F) ->
{ok,B} = file:read_file(B),
B.

This is not much more work, and has the advantage of giving
clearer exit messages (very much so in the past - the newest emulator
has much improved error messages.)

The style of matching {ok,X} = f(..) used to have the disadvantage
that the exit message became {badmatch, error}, which was really
pretty useless if you had several such assertions in the same function.

As for the begin ... end construct, I've found it useful in
combination with functions which always exit. I often want the
top level of a complex flow to be "nice" to the user, but prefer
the components to be distinct -- they either succeed or exit.

reorganize_hard_drive(D) ->
case catch (begin
P = read_prefs(),
Fs = scan_disk(D, P),
purge_old(Fs, P),
optimize(D, Fs, P)
end) of
{'EXIT', Reason} ->
cleanup(D),
format_error(Reason),
{error, Reason};
Res -> Res
end.

/Uffe
Ulf Wiger, Chief Designer AXD 301 <ulf.wiger_at_etxb.ericsson.se>
Ericsson Telecom AB tfn: +46 8 719 81 95
Varuv

Display posts from previous:  

All times are GMT
Page 2 of 2
Goto page Previous  1, 2
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum