[erlang-questions] Exceptions vs Tagged Values (was Package Support/Use)

Christian S chsu79@REDACTED
Tue Nov 7 16:59:35 CET 2006


> > The original statement was that tuple tagging is bad style, not
> > criticism of exceptions. Exceptions are fine and should be used when
> > appropriate, but I personally dont find that exceptions are an
> > argument against tuple tagged return values.
>
> No, the argument against tagged values is that they are complicated and
> astonishing return types. file:open, for example, should return a file
> descriptor.

To go further off-topic on an off-subject discussion:

For file access I prefer inspiration from with-* functions in lisp. That is

with_open_file(Name, Mode, Fun) when is_function(Fun, 1) ->
  {ok, File} = open(Name, Mode),
  try
    Fun(File)
  after
    close(File)
  end.

This function could be rewritten to throw more informative exceptions
on various error cases. This API makes it natural to use exceptions
when a Fun cant be called with the open file. The programmer has all
intent that Fun should be called with an open FD and no intent for the
file to not exist.



More information about the erlang-questions mailing list