[m-users.] Is there a way to make io functions throw an exception, instead of returning io.res

Charles Shuller charles.shuller at gmail.com
Sat Mar 30 03:18:31 AEDT 2019


For my current project, I will never attempt to recover from an error, and
just want to fail reporting the error.

Most io function return an io.res, or something closely related, which
needs checking instead of throwing an exception.   And I get a LOT of
repetitive, error handling code cluttering up my predicate bodies.

Is there some way I can box those functions up to throw an exception on
error, instead?   Most of my code looks a lot like:

remove_file_recursively(Path, !IO) :-
  io.remove_file_recursively(Path, Res, !IO),
  (
    Res = ok
  ;
    Res = error(Error),
    io.error_message(Error, ErrorString),
    string.format("Could not recursively remove %s: %s", [s(Path),
s(ErrorString)], ExceptionMessage),
    exception.throw(ExceptionMessage)
  ).

With only minor variations for the position of Res in the io function call,
and the actual Result type (io.res, io.res(T), io.result, etc.....).   So
it SEEMS like it might be possible to write a predicate like:
io_det(io.remove_file_recursively,
Path, !IO)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20190329/2b7f81ca/attachment.html>


More information about the users mailing list