<html dir="ltr"><head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body style="text-align:left; direction:ltr;"><div>Am Freitag, dem 04.02.2022 um 11:50 +0100 schrieb Volker Wysk:</div><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><br><div>Am Freitag, dem 04.02.2022 um 10:49 +0100 schrieb fabrice nicol:</div><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><br><blockquote type="cite" cite="mid:026071b30d0ce46a48154847697e754c533d4907.camel@volker-wysk.de" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><blockquote type="cite" cite="mid:9e86fd24e8505d3644ad14cba311bbf7fbb786f3.camel@volker-wysk.de" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><br><pre>I want to read the contents of a file, using open_input,</pre><pre>read_file_as_string, close_input. In case the file doesn't exist, I want to</pre><pre>return an empty string. So, in case of an error, I want to determine if it's</pre><pre>because the file doesn't exist.</pre><pre>It isn't a show stopper to me, I can return "" in case of any error, but it</pre><pre>isn't optimal.</pre><pre>Cheers,</pre><pre>Volker</pre></blockquote><p>Why do you need to test according to io.error? <br></p><p>io.error will only "materialize" logical failure. Which takes place further up in the tree.</p></blockquote><div>I don't get it.</div></blockquote><p>This looks clear to me at least. io.error does not pop up out of thin air.</p><p>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,</p><p>    OpenInputResult = ok(InputStream)</p><p>fails. Then the other branch of the disjunct is processed and you get the error message through:<br></p><p>    OpenInputResult = error(OpenInputError)</p><p>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).</p></blockquote><div>This is what I'm doing:</div><div><br></div><div>% Read an entire text file. Return the result and errors in an io.res.</div><div>:- pred contents(string::in,         % File name</div><div>                 io.res(string)::out,     % Result (the contents or an error)</div><div>                 io::di, io::uo)</div><div>   is det.</div><div></div><div><br></div><div>contents(Filename, Res, !IO) :-</div><div>    open_input(Filename, Res1, !IO),</div><div>    (</div><div>        Res1 = ok(Stream),</div><div><br></div><div>        read_file_as_string(Stream, Res2 : io.maybe_partial_res(string), !IO),</div><div>        (</div><div>            Res2 = ok(Contents),</div><div>            Res = ok(Contents)</div><div>        ;</div><div>            Res2 = error(_, Err2),</div><div>            Res = error(Err2)</div><div>        ),</div><div>        close_input(Stream, !IO)</div><div>    ;</div><div>        Res1 = error(Error : io.error),</div><div>        Res = io.error(Error)</div><div>    ).</div></blockquote><div><br></div><div>And this is the way I'm using it right now:</div><div><br></div><div>    contents(Pagesdir ++ "/" ++ Dirname ++ "/current", Res2, !IO),</div><div>    (</div><div>      Res2 = ok(Current0),</div><div>      Current = chomp(Current0)</div><div>    ;</div><div>      Res2 = error(_Err2),</div><div>      Current = ""</div><div>      % How to inspect the error..?? "eNOENT" is from posix, not io, so this doesn't work:</div><div>      % ( if   Err2 = eNOENT</div><div>      %   then Current = ""</div><div>      %   else strerror(Err2, Msg2, !IO),</div><div>      %        throw(Revdir ++ ": " ++ Msg2)</div><div>      % )</div><div>    ),</div><div></div><div><br></div><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><div></div><div><br></div><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><blockquote type="cite" cite="mid:026071b30d0ce46a48154847697e754c533d4907.camel@volker-wysk.de" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><blockquote type="cite" style="margin:0 0 0 .8ex; border-left:2px #729fcf solid;padding-left:1ex"><p>So you just need to test against open_input out-mode argument values.<br></p><p>Have you tried something along these lines?</p></blockquote><div>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. </div></blockquote><p>In this case, you can return ReadInputError instead of ResultStr (or throwing an exception in the second disjunct after read_file_as_string).</p><p>And analyse ReadInputError later (just modify my_pred accordingly).</p></blockquote><pre>No, you can't. The ReadInputError is of type io.error, and this is abstract. The only thing you can do with it, is to get an error message. That's the problem here.</pre><br><pre>Cheers, Volker</pre>
<pre>_______________________________________________</pre><pre>users mailing list</pre><a href="mailto:users@lists.mercurylang.org"><pre>users@lists.mercurylang.org</pre></a><pre><br></pre><a href="https://lists.mercurylang.org/listinfo/users"><pre>https://lists.mercurylang.org/listinfo/users</pre></a><pre><br></pre></blockquote></body></html>