[m-users.] io.error type

fabrice nicol fabrnicol at gmail.com
Fri Feb 4 20:49:41 AEDT 2022


>>>
>>> I want to read the contents of a file, using open_input,
>>> read_file_as_string, close_input. In case the file doesn't exist, I want to
>>> return an empty string. So, in case of an error, I want to determine if it's
>>> because the file doesn't exist.
>>> It isn't a show stopper to me, I can return "" in case of any error, but it
>>> isn't optimal.
>>> Cheers,
>>> Volker
>>
>> Why do you need to test according to io.error?
>>
>> io.error will only "materialize" logical failure. Which takes place 
>> further up in the tree.
>>
> I don't get it.

This looks clear to me at least. io.error does not pop up out of thin air.

You get an io.error when there is an error in open_input. In the snippet 
I sent, when there is an error on opening the file,

     OpenInputResult = ok(InputStream)

fails. Then the other branch of the disjunct is processed and you get 
the error message through:

     OpenInputResult = error(OpenInputError)

Now, there is no use testing against io.error, as long as you can test 
against OpenInputResult, which is unified before the disjunction is 
processed (one node up relative to the nodes of the above disjunct 
unifications).

>> So you just need to test against open_input out-mode argument values.
>>
>> Have you tried something along these lines?
>>
> I need to distinguish between "file does not exist" errors and other 
> errors. I want the other errors stay errors, which will get reported 
> later.

In this case, you can return ReadInputError instead of ResultStr (or 
throwing an exception in the second disjunct after read_file_as_string).

And analyse ReadInputError later (just modify my_pred accordingly).

> Only "file does not exist" errors should be ignored.
Fabrice
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20220204/00876871/attachment-0001.html>


More information about the users mailing list