global_name_server question.

Ulf Wiger ulf.wiger@REDACTED
Sun Jun 6 15:50:14 CEST 1999


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@REDACTED>
Ericsson Telecom AB                          tfn: +46  8 719 81 95
Varuvägen 9, Älvsjö                          mob: +46 70 519 81 95
S-126 25 Stockholm, Sweden                   fax: +46  8 719 43 44




More information about the erlang-questions mailing list