man-pages/man3/Result.3o.html
2021-03-31 01:06:50 +01:00

639 lines
5.7 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of Result</TITLE>
</HEAD><BODY>
<H1>Result</H1>
Section: OCaml library (3o)<BR>Updated: 2020-01-30<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
Result - Result values.
<A NAME="lbAC">&nbsp;</A>
<H2>Module</H2>
Module Result
<A NAME="lbAD">&nbsp;</A>
<H2>Documentation</H2>
<P>
Module
<B>Result</B>
<BR>&nbsp;:&nbsp;
<B>sig end</B>
<P>
<P>
Result values.
<P>
Result values handle computation results and errors in an explicit
and declarative manner without resorting to exceptions.
<P>
<P>
<B>Since</B>
4.08
<P>
<P>
<P>
<P>
<P>
<P>
<P>
<A NAME="lbAE">&nbsp;</A>
<H3>Results</H3>
<P>
<P>
<I>type </I>
<B>('a, 'e)</B>
<I>t </I>
=
<B>('a, 'e) result</B>
=
<BR>&nbsp;|&nbsp;Ok
<B>of </B>
<B>'a</B>
<BR>&nbsp;|&nbsp;Error
<B>of </B>
<B>'e</B>
<BR>&nbsp;
<P>
The type for result values. Either a value
<B>Ok v</B>
or an error
<B>Error e</B>
.
<P>
<P>
<P>
<I>val ok </I>
:
<B>'a -&gt; ('a, 'e) result</B>
<P>
<P>
<B>ok v</B>
is
<B>Ok v</B>
.
<P>
<P>
<P>
<I>val error </I>
:
<B>'e -&gt; ('a, 'e) result</B>
<P>
<P>
<B>error e</B>
is
<B>Error e</B>
.
<P>
<P>
<P>
<I>val value </I>
:
<B>('a, 'e) result -&gt; default:'a -&gt; 'a</B>
<P>
<P>
<B>value r ~default</B>
is
<B>v</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<B>default</B>
otherwise.
<P>
<P>
<P>
<I>val get_ok </I>
:
<B>('a, 'e) result -&gt; 'a</B>
<P>
<P>
<B>get_ok r</B>
is
<B>v</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<P>
<P>
<B>Raises Invalid_argument</B>
otherwise.
<P>
<P>
<P>
<I>val get_error </I>
:
<B>('a, 'e) result -&gt; 'e</B>
<P>
<P>
<B>get_error r</B>
is
<B>e</B>
if
<B>r</B>
is
<B>Error e</B>
and
<P>
<P>
<B>Raises Invalid_argument</B>
otherwise.
<P>
<P>
<P>
<I>val bind </I>
:
<B>('a, 'e) result -&gt;</B>
<B>('a -&gt; ('b, 'e) result) -&gt; ('b, 'e) result</B>
<P>
<P>
<B>bind r f</B>
is
<B>f v</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<B>r</B>
if
<B>r</B>
is
<B>Error _</B>
.
<P>
<P>
<P>
<I>val join </I>
:
<B>(('a, 'e) result, 'e) result -&gt; ('a, 'e) result</B>
<P>
<P>
<B>join rr</B>
is
<B>r</B>
if
<B>rr</B>
is
<B>Ok r</B>
and
<B>rr</B>
if
<B>rr</B>
is
<B>Error _</B>
.
<P>
<P>
<P>
<I>val map </I>
:
<B>('a -&gt; 'b) -&gt; ('a, 'e) result -&gt; ('b, 'e) result</B>
<P>
<P>
<B>map f r</B>
is
<B>Ok (f v)</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<B>r</B>
if
<B>r</B>
is
<B>Error _</B>
.
<P>
<P>
<P>
<I>val map_error </I>
:
<B>('e -&gt; 'f) -&gt; ('a, 'e) result -&gt; ('a, 'f) result</B>
<P>
<P>
<B>map_error f r</B>
is
<B>Error (f e)</B>
if
<B>r</B>
is
<B>Error e</B>
and
<B>r</B>
if
<B>r</B>
is
<B>Ok _</B>
.
<P>
<P>
<P>
<I>val fold </I>
:
<B>ok:('a -&gt; 'c) -&gt; error:('e -&gt; 'c) -&gt; ('a, 'e) result -&gt; 'c</B>
<P>
<P>
<B>fold ~ok ~error r</B>
is
<B>ok v</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<B>error e</B>
if
<B>r</B>
is
<B>Error e</B>
.
<P>
<P>
<P>
<I>val iter </I>
:
<B>('a -&gt; unit) -&gt; ('a, 'e) result -&gt; unit</B>
<P>
<P>
<B>iter f r</B>
is
<B>f v</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<B>()</B>
otherwise.
<P>
<P>
<P>
<I>val iter_error </I>
:
<B>('e -&gt; unit) -&gt; ('a, 'e) result -&gt; unit</B>
<P>
<P>
<B>iter_error f r</B>
is
<B>f e</B>
if
<B>r</B>
is
<B>Error e</B>
and
<B>()</B>
otherwise.
<P>
<P>
<P>
<P>
<A NAME="lbAF">&nbsp;</A>
<H3>Predicates and comparisons</H3>
<P>
<P>
<P>
<I>val is_ok </I>
:
<B>('a, 'e) result -&gt; bool</B>
<P>
<P>
<B>is_ok r</B>
is
<B>true</B>
iff
<B>r</B>
is
<B>Ok _</B>
.
<P>
<P>
<P>
<I>val is_error </I>
:
<B>('a, 'e) result -&gt; bool</B>
<P>
<P>
<B>is_error r</B>
is
<B>true</B>
iff
<B>r</B>
is
<B>Error _</B>
.
<P>
<P>
<P>
<I>val equal </I>
:
<B>ok:('a -&gt; 'a -&gt; bool) -&gt;</B>
<B>error:('e -&gt; 'e -&gt; bool) -&gt;</B>
<B>('a, 'e) result -&gt; ('a, 'e) result -&gt; bool</B>
<P>
<P>
<B>equal ~ok ~error r0 r1</B>
tests equality of
<B>r0</B>
and
<B>r1</B>
using
<B>ok</B>
and
<B>error</B>
to respectively compare values wrapped by
<B>Ok _</B>
and
<B>Error _</B>
.
<P>
<P>
<P>
<I>val compare </I>
:
<B>ok:('a -&gt; 'a -&gt; int) -&gt;</B>
<B>error:('e -&gt; 'e -&gt; int) -&gt;</B>
<B>('a, 'e) result -&gt; ('a, 'e) result -&gt; int</B>
<P>
<P>
<B>compare ~ok ~error r0 r1</B>
totally orders
<B>r0</B>
and
<B>r1</B>
using
<B>ok</B>
and
<B>error</B>
to respectively compare values wrapped by
<B>Ok _ </B>
and
<B>Error _</B>
.
<B>Ok _</B>
values are smaller than
<B>Error _</B>
values.
<P>
<P>
<P>
<P>
<A NAME="lbAG">&nbsp;</A>
<H3>Converting</H3>
<P>
<P>
<P>
<I>val to_option </I>
:
<B>('a, 'e) result -&gt; 'a option</B>
<P>
<P>
<B>to_option r</B>
is
<B>r</B>
as an option, mapping
<B>Ok v</B>
to
<B>Some v</B>
and
<B>Error _</B>
to
<B>None</B>
.
<P>
<P>
<P>
<I>val to_list </I>
:
<B>('a, 'e) result -&gt; 'a list</B>
<P>
<P>
<B>to_list r</B>
is
<B>[v]</B>
if
<B>r</B>
is
<B>Ok v</B>
and
<B>[]</B>
otherwise.
<P>
<P>
<P>
<I>val to_seq </I>
:
<B>('a, 'e) result -&gt; 'a Seq.t</B>
<P>
<P>
<B>to_seq r</B>
is
<B>r</B>
as a sequence.
<B>Ok v</B>
is the singleton sequence
containing
<B>v</B>
and
<B>Error _</B>
is the empty sequence.
<P>
<P>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">Module</A><DD>
<DT id="3"><A HREF="#lbAD">Documentation</A><DD>
<DL>
<DT id="4"><A HREF="#lbAE">Results</A><DD>
<DT id="5"><A HREF="#lbAF">Predicates and comparisons</A><DD>
<DT id="6"><A HREF="#lbAG">Converting</A><DD>
</DL>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:54 GMT, March 31, 2021
</BODY>
</HTML>