8 Commits

Author SHA1 Message Date
30c9acc2bb Version 2.0.0 2020-07-07 15:53:00 +02:00
16ecac7329 Add Either type 2020-07-04 16:25:29 +02:00
f66c5e7f18 Update README 2020-07-02 20:10:37 +02:00
22bd9025fb Add README 2020-07-02 20:05:04 +02:00
8f27c5e552 Add fail templates 2020-07-02 19:59:01 +02:00
cd80913346 Update docs 2020-07-02 18:39:13 +02:00
cb5951794d Update docs 2020-07-02 18:11:06 +02:00
2f1f5ed2f8 Add alias for "fail" 2020-07-02 18:10:48 +02:00
6 changed files with 348 additions and 90 deletions

3
.gitignore vendored
View File

@@ -1 +1,2 @@
*.exe
*.exe
.vscode/

23
README.md Normal file
View File

@@ -0,0 +1,23 @@
# op
Generic, exception-free return values.
OP stands for "Operation Result".
This module contains a generic type that can be used as a return type for operations that could fail. It adds additional messages to the result.
This improves upon the [options module](https://nim-lang.org/docs/options.html) in that additional messages can be passed along with the presence or absence of a value.
## Basic Usage
``` nim
import op
proc divide(a, b: int): OP[float] =
## This could fail
if b == 0:
return fail "Cannot divide by zero!"
else:
return ok a / b # Wrap the result
let r = divide(42, 0)
assert r.isOk == false
assert r.error == "Cannot divide by zero!"
```

View File

@@ -97,29 +97,50 @@ function main() {
</select>
</div>
<ul class="simple simple-toc" id="toc-list">
<li><a class="reference" id="basic-usage_toc" href="#basic-usage">Basic Usage</a></li>
<li>
<a class="reference reference-toplevel" href="#7" id="57">Types</a>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#OP"
title="OP[T] = object of RootObj
case isOk*: bool
<li><a class="reference" href="#Either"
title="Either[A; B] = object
case isA*: bool
of true:
val*: T
a*: A
of false:
error*: string"><wbr />OP<span class="attachedType"></span></a></li>
b*: B"><wbr />Either<span class="attachedType"></span></a></li>
<li><a class="reference" href="#OP"
title="OP[T] = Either[T, string]"><wbr />OP<span class="attachedType"></span></a></li>
</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#12" id="62">Procs</a>
<a class="reference reference-toplevel" href="#18" id="68">Templates</a>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#ok%2CT"
<li><a class="reference" href="#checkA.t%2CEither"
title="checkA(self: Either): bool"><wbr />check<wbr />A<span class="attachedType">Either</span></a></li>
<li><a class="reference" href="#getA.t%2CEither%5BA%2CB%5D"
title="getA[A; B](self: Either[A, B]): A"><wbr />get<wbr />A<span class="attachedType">Either</span></a></li>
<li><a class="reference" href="#getB.t%2CEither%5BA%2CB%5D"
title="getB[A; B](self: Either[A, B]): B"><wbr />get<wbr />B<span class="attachedType">Either</span></a></li>
<li><a class="reference" href="#isOK.t%2COP"
title="isOK(self: OP): bool"><wbr />is<wbr />OK<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#val.t%2COP"
title="val(self: OP): auto"><wbr />val<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#error.t%2COP"
title="error(self: OP): auto"><wbr />error<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#ok.t%2CT"
title="ok[T](val: T): OP[T]"><wbr />ok<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#fail%2COP%2Cstring"
<li><a class="reference" href="#ok.t%2COP%5BT%5D%2CT"
title="ok[T](self: var OP[T]; val: T)"><wbr />ok<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#fail.t%2C%2Cstring"
title="fail[T; ](O: type OP[T]; msg: string): O:type"><wbr />fail<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#fail.t%2Ctypedesc%2Cstring"
title="fail(T: typedesc; msg: string): OP[T]"><wbr />fail<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#fail.t%2COP%2Cstring"
title="fail(op: OP; msg: string): OP"><wbr />fail<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#fail%2Cstring"
title="fail[T](msg: string): OP[T]"><wbr />fail<span class="attachedType">OP</span></a></li>
<li><a class="reference" href="#fail.t%2Cstatic%5Bstring%5D"
title="fail(msg: static[string]): auto"><wbr />fail<span class="attachedType"></span></a></li>
</ul>
</li>
@@ -130,25 +151,47 @@ function main() {
<div class="nine columns" id="content">
<div id="tocRoot"></div>
<p class="module-desc"><p>This module contains a generic type that can be used as a return type for operations that could fail. It adds additional messages to the result.</p>
<p>OP stands for &quot;Operation result&quot;.</p>
</p>
<p class="module-desc"><table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Version:</th><td> 1.0.0</td></tr>
<tr><th class="docinfo-name">Author:</th><td> luxick &lt;op@luxick.de&gt;</td></tr>
</tbody></table><p>OP stands for &quot;Operation Result&quot;.</p>
<p>This module contains a generic type that can be used as a return type for operations that could fail. It adds additional messages to the result.</p>
<p>This module improves upon the <a class="reference external" href="https://nim-lang.org/docs/options.html">options module</a> in that additional messages can be passed along with the presence or absence of a value.</p>
<h1><a class="toc-backref" id="basic-usage" href="#basic-usage">Basic Usage</a></h1><pre class="listing"><span class="Keyword">proc</span> <span class="Identifier">divide</span><span class="Punctuation">(</span><span class="Identifier">a</span><span class="Punctuation">,</span> <span class="Identifier">b</span><span class="Punctuation">:</span> <span class="Identifier">int</span><span class="Punctuation">)</span><span class="Punctuation">:</span> <span class="Identifier">OP</span><span class="Punctuation">[</span><span class="Identifier">float</span><span class="Punctuation">]</span> <span class="Operator">=</span>
<span class="Comment">## This could fail</span>
<span class="Keyword">if</span> <span class="Identifier">b</span> <span class="Operator">==</span> <span class="DecNumber">0</span><span class="Punctuation">:</span>
<span class="Keyword">return</span> <span class="Identifier">fail</span> <span class="StringLit">&quot;Cannot divide by zero!&quot;</span>
<span class="Keyword">else</span><span class="Punctuation">:</span>
<span class="Keyword">return</span> <span class="Identifier">ok</span> <span class="Identifier">a</span> <span class="Operator">/</span> <span class="Identifier">b</span> <span class="Comment"># Wrap the result</span>
<span class="Keyword">let</span> <span class="Identifier">r</span> <span class="Operator">=</span> <span class="Identifier">divide</span><span class="Punctuation">(</span><span class="DecNumber">42</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">)</span>
<span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">isOk</span> <span class="Operator">==</span> <span class="Identifier">false</span>
<span class="Identifier">assert</span> <span class="Identifier">r</span><span class="Operator">.</span><span class="Identifier">error</span> <span class="Operator">==</span> <span class="StringLit">&quot;Cannot divide by zero!&quot;</span></pre></p>
<div class="section" id="7">
<h1><a class="toc-backref" href="#7">Types</a></h1>
<dl class="item">
<a id="OP"></a>
<dt><pre><a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span> <span class="Keyword">of</span> <span class="Identifier">RootObj</span>
<span class="Keyword">case</span> <span class="Identifier">isOk</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">bool</span>
<a id="Either"></a>
<dt><pre><a href="op.html#Either"><span class="Identifier">Either</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">B</span><span class="Other">]</span> <span class="Other">=</span> <span class="Keyword">object</span>
<span class="Keyword">case</span> <span class="Identifier">isA</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">bool</span>
<span class="Keyword">of</span> <span class="Identifier">true</span><span class="Other">:</span>
<span class="Identifier">val</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">T</span>
<span class="Identifier">a</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">A</span>
<span class="Keyword">of</span> <span class="Identifier">false</span><span class="Other">:</span>
<span class="Identifier">error</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">string</span>
<span class="Identifier">b</span><span class="Operator">*</span><span class="Other">:</span> <span class="Identifier">B</span>
</pre></dt>
<dd>
Object to wrap the result of an operation<ul class="simple"><li><tt class="docutils literal"><span class="pre">isOk</span></tt>: Indicates if the operation was successful</li>
An <tt class="docutils literal"><span class="pre">Either</span></tt> object can hold either one value or the other, but never both
</dd>
<a id="OP"></a>
<dt><pre><a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span> <span class="Other">=</span> <a href="op.html#Either"><span class="Identifier">Either</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">,</span> <span class="Identifier">string</span><span class="Other">]</span></pre></dt>
<dd>
<p>Object to wrap the result of an operation. Alias of <tt class="docutils literal"><span class="pre">Either</span></tt> with a string as error message</p>
<p>The type is discriminated by the <tt class="docutils literal"><span class="pre">isOK</span></tt> bool. So it is an compiler error to try to access the value without checking if the operation was successful.</p>
<ul class="simple"><li><tt class="docutils literal"><span class="pre">isOk</span></tt>: Indicates if the operation was successful</li>
<li><tt class="docutils literal"><span class="pre">val</span></tt>: If successful, this will hold the real result value</li>
<li><tt class="docutils literal"><span class="pre">error</span></tt>: Otherwise this will hold an error message</li>
</ul>
@@ -157,18 +200,91 @@ Object to wrap the result of an operation<ul class="simple"><li><tt class="docut
</dd>
</dl></div>
<div class="section" id="12">
<h1><a class="toc-backref" href="#12">Procs</a></h1>
<div class="section" id="18">
<h1><a class="toc-backref" href="#18">Templates</a></h1>
<dl class="item">
<a id="ok,T"></a>
<dt><pre><span class="Keyword">proc</span> <a href="#ok%2CT"><span class="Identifier">ok</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<a id="checkA.t,Either"></a>
<dt><pre><span class="Keyword">template</span> <a href="#checkA.t%2CEither"><span class="Identifier">checkA</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="op.html#Either"><span class="Identifier">Either</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">bool</span></pre></dt>
<dd>
</dd>
<a id="getA.t,Either[A,B]"></a>
<dt><pre><span class="Keyword">template</span> <a href="#getA.t%2CEither%5BA%2CB%5D"><span class="Identifier">getA</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="op.html#Either"><span class="Identifier">Either</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">A</span></pre></dt>
<dd>
</dd>
<a id="getB.t,Either[A,B]"></a>
<dt><pre><span class="Keyword">template</span> <a href="#getB.t%2CEither%5BA%2CB%5D"><span class="Identifier">getB</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">;</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="op.html#Either"><span class="Identifier">Either</span></a><span class="Other">[</span><span class="Identifier">A</span><span class="Other">,</span> <span class="Identifier">B</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">B</span></pre></dt>
<dd>
</dd>
<a id="isOK.t,OP"></a>
<dt><pre><span class="Keyword">template</span> <a href="#isOK.t%2COP"><span class="Identifier">isOK</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">bool</span></pre></dt>
<dd>
</dd>
<a id="val.t,OP"></a>
<dt><pre><span class="Keyword">template</span> <a href="#val.t%2COP"><span class="Identifier">val</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">auto</span></pre></dt>
<dd>
</dd>
<a id="error.t,OP"></a>
<dt><pre><span class="Keyword">template</span> <a href="#error.t%2COP"><span class="Identifier">error</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">auto</span></pre></dt>
<dd>
</dd>
<a id="ok.t,T"></a>
<dt><pre><span class="Keyword">template</span> <a href="#ok.t%2CT"><span class="Identifier">ok</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>
Wraps the given value in a successful operation result.
</dd>
<a id="fail,OP,string"></a>
<dt><pre><span class="Keyword">proc</span> <a href="#fail%2COP%2Cstring"><span class="Identifier">fail</span></a><span class="Other">(</span><span class="Identifier">op</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">;</span> <span class="Identifier">msg</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">OP</span></pre></dt>
<a id="ok.t,OP[T],T"></a>
<dt><pre><span class="Keyword">template</span> <a href="#ok.t%2COP%5BT%5D%2CT"><span class="Identifier">ok</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <span class="Keyword">var</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">val</span><span class="Other">:</span> <span class="Identifier">T</span><span class="Other">)</span></pre></dt>
<dd>
Set the result to the given value
</dd>
<a id="fail.t,,string"></a>
<dt><pre><span class="Keyword">template</span> <a href="#fail.t%2C%2Cstring"><span class="Identifier">fail</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">;</span> <span class="Other">]</span><span class="Other">(</span><span class="Identifier">O</span><span class="Other">:</span> <span class="Keyword">type</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">;</span> <span class="Identifier">msg</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">O:type</span></pre></dt>
<dd>
<p>Will create a new operation result with the given error message. The type for the operation result is given explicitly.</p>
<p><strong>See Also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#fail,OP,string">fail proc</a></li>
<li><a class="reference external" href="#fail.t,static[string]">fail template</a></li>
</ul>
</dd>
<a id="fail.t,typedesc,string"></a>
<dt><pre><span class="Keyword">template</span> <a href="#fail.t%2Ctypedesc%2Cstring"><span class="Identifier">fail</span></a><span class="Other">(</span><span class="Identifier">T</span><span class="Other">:</span> <span class="Identifier">typedesc</span><span class="Other">;</span> <span class="Identifier">msg</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<dd>
<p>Will create a new operation result with the given error message. The type for the operation result is given explicitly.</p>
<p><strong>See Also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#fail,OP,string">fail proc</a></li>
<li><a class="reference external" href="#fail.t,static[string]">fail template</a></li>
</ul>
</dd>
<a id="fail.t,OP,string"></a>
<dt><pre><span class="Keyword">template</span> <a href="#fail.t%2COP%2Cstring"><span class="Identifier">fail</span></a><span class="Other">(</span><span class="Identifier">op</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">;</span> <span class="Identifier">msg</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">OP</span></pre></dt>
<dd>
Will create a new operation result with the given error message. The type for the operation result is taken from the <tt class="docutils literal"><span class="pre">op</span></tt> argument.
@@ -181,19 +297,11 @@ Will create a new operation result with the given error message. The type for th
<span class="Identifier">assert</span> <span class="Identifier">data</span><span class="Other">.</span><span class="Identifier">error</span> <span class="Operator">==</span> <span class="StringLit">&quot;Not implemented!&quot;</span></pre>
</dd>
<a id="fail,string"></a>
<dt><pre><span class="Keyword">proc</span> <a href="#fail%2Cstring"><span class="Identifier">fail</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span><span class="Other">(</span><span class="Identifier">msg</span><span class="Other">:</span> <span class="Identifier">string</span><span class="Other">)</span><span class="Other">:</span> <a href="op.html#OP"><span class="Identifier">OP</span></a><span class="Other">[</span><span class="Identifier">T</span><span class="Other">]</span></pre></dt>
<a id="fail.t,static[string]"></a>
<dt><pre><span class="Keyword">template</span> <a href="#fail.t%2Cstatic%5Bstring%5D"><span class="Identifier">fail</span></a><span class="Other">(</span><span class="Identifier">msg</span><span class="Other">:</span> <span class="Identifier">static</span><span class="Other">[</span><span class="Identifier">string</span><span class="Other">]</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">auto</span></pre></dt>
<dd>
<p>Will create a new operation result with the given error message. The type for the operation result is given explicitly.</p>
<p><strong>See Also:</strong></p>
<ul class="simple"><li><a class="reference external" href="#fail,OP,string">fail proc</a></li>
</ul>
<p><strong class="examples_text">Examples:</strong></p>
<pre class="listing"><span class="Keyword">let</span> <span class="Identifier">res</span> <span class="Other">=</span> <span class="Identifier">fail</span><span class="Other">[</span><span class="Identifier">seq</span><span class="Other">[</span><span class="Identifier">float</span><span class="Other">]</span><span class="Other">]</span> <span class="StringLit">&quot;Something is wrong!&quot;</span>
<span class="Identifier">assert</span> <span class="Identifier">res</span><span class="Other">.</span><span class="Identifier">isOk</span> <span class="Operator">==</span> <span class="Identifier">false</span>
<span class="Identifier">assert</span> <span class="Identifier">res</span><span class="Other">.</span><span class="Identifier">error</span> <span class="Operator">==</span> <span class="StringLit">&quot;Something is wrong!&quot;</span></pre>
</dd>
@@ -206,7 +314,7 @@ Will create a new operation result with the given error message. The type for th
<div class="twelve-columns footer">
<span class="nim-sprite"></span>
<br/>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-01 21:23:47 UTC</small>
<small style="color: var(--hint);">Made with Nim. Generated: 2020-07-07 13:52:22 UTC</small>
</div>
</div>
</div>

View File

@@ -10,5 +10,9 @@ srcDir = "src"
requires "nim >= 1.2.4"
task genDocs, "Generate Docs":
task docs, "Generate Docs":
exec "nim doc --project -o:doc src/op.nim"
task bench, "Runs the benchmark code":
exec "nim c -r benchmarks/bench.nim"

View File

@@ -1,26 +1,86 @@
## :Version: 1.0.0
## :Author: luxick <op@luxick.de>
##
## OP stands for "Operation Result".
##
## This module contains a generic type that can be used as a return type
## for operations that could fail. It adds additional messages to the result.
##
## OP stands for "Operation result".
##
## This module improves upon the `options module<https://nim-lang.org/docs/options.html>`_
## in that additional messages can be passed along with the presence or absence of a value.
##
## Basic Usage
## -----------
## .. code-block:: nim
##
## proc divide(a, b: int): OP[float] =
## ## This could fail
## if b == 0:
## return fail "Cannot divide by zero!"
## else:
## return ok a / b # Wrap the result
##
## let r = divide(42, 0)
## assert r.isOk == false
## assert r.error == "Cannot divide by zero!"
type
OP*[T] = object of RootObj
## Object to wrap the result of an operation
Either*[A, B] = object
## An `Either` object can hold either one value or the other, but never both
case isA*: bool
of true:
a*: A
of false:
b*: B
OP*[T] = Either[T, string]
## Object to wrap the result of an operation.
## Alias of `Either` with a string as error message
##
## The type is discriminated by the `isOK` bool.
## So it is an compiler error to try to access the value without checking
## if the operation was successful.
##
## - `isOk`: Indicates if the operation was successful
## - `val`: If successful, this will hold the real result value
## - `error`: Otherwise this will hold an error message
case isOk*: bool
of true:
val*: T
of false:
error*: string
proc ok*[T](val: T): OP[T] =
template checkA*(self: Either): bool = self.isA
template getA*[A, B](self: Either[A, B]): A = self.a
template getB*[A, B](self: Either[A, B]): B = self.b
template isOK*(self: OP): bool = checkA(self)
template val*(self: OP): auto = getA(self)
template error*(self: OP): auto = getB(self)
template ok*[T](val: T): OP[T] =
## Wraps the given value in a successful operation result.
OP[T](isOK: true, val: val)
OP[T](isA: true, a: val)
proc fail*(op: OP, msg: string): OP =
template ok*[T](self: var OP[T], val: T) =
## Set the result to the given value
self = ok val
template fail*[T](O: type OP[T], msg: string): O =
## Will create a new operation result with the given error message.
## The type for the operation result is given explicitly.
##
## **See Also:**
## - `fail proc<#fail,OP,string>`_
## - `fail template<#fail.t,static[string]>`_
OP[T](isA: false, b: msg)
template fail*(T: typedesc, msg: string): OP[T] =
## Will create a new operation result with the given error message.
## The type for the operation result is given explicitly.
##
## **See Also:**
## - `fail proc<#fail,OP,string>`_
## - `fail template<#fail.t,static[string]>`_
OP[typeof(T)](isA: false, b: msg)
template fail*(op: OP, msg: string): OP =
## Will create a new operation result with the given error message.
## The type for the operation result is taken from the `op` argument.
runnableExamples:
@@ -30,18 +90,6 @@ proc fail*(op: OP, msg: string): OP =
let data = someProc()
assert data.isOk == false
assert data.error == "Not implemented!"
OP(isOK: false, error: msg)
proc fail*[T](msg: string): OP[T] =
## Will create a new operation result with the given error message.
## The type for the operation result is given explicitly.
##
## **See Also:**
## - `fail proc
## <#fail,OP,string>`_
runnableExamples:
let res = fail[seq[float]] "Something is wrong!"
assert res.isOk == false
assert res.error == "Something is wrong!"
OP[T](isOK: false, error: msg)
fail(typeof(op), msg)
template fail*(msg: static[string]): auto = fail(typeof(result), msg)

View File

@@ -3,36 +3,110 @@
import unittest
import op
test "Check OK":
let test = ok 1
check test.isOk == true
suite "Basic tests":
proc divide(a, b: int): OP[float] =
if b == 0:
return fail "Cannot divide by zero!"
else:
return ok a / b
test "Check fail":
let test = op.fail[int] "no data here"
check test.isOk == false
test "Check OK":
let test = ok 1
check test.isOk == true
test "Check proc results":
proc createValue: OP[string] =
let myString = "This is test code!"
ok myString
let data = createValue()
check data.isOk
check data.val == "This is test code!"
test "Check fail":
let test = int.fail "no data here"
check test.isOk == false
test "Check failing proc":
proc someProc(): OP[int] =
result.fail "Not implemented!"
test "Check proc results":
proc createValue: OP[string] =
let myString = "This is test code!"
ok myString
let r = createValue()
check r.isOk
check r.val == "This is test code!"
let data = someProc()
assert data.isOk == false
assert data.error == "Not implemented!"
test "Check failing result proc":
proc someProc(): OP[int] =
result.fail "Not implemented!"
test "Check changing result":
proc checker(): OP[int] =
result = ok 42
# something happend here
result = result.fail "data got corrupted"
let data = someProc()
assert data.isOk == false
assert data.error == "Not implemented!"
let data = checker()
check data.isOk == false
check data.error == "data got corrupted"
test "Check failing typedesc proc ":
proc someProc(): OP[int] =
op.fail(int, "Not implemented!")
let data = someProc()
assert data.isOk == false
assert data.error == "Not implemented!"
test "Check failing type param proc ":
proc someProc(): OP[int] =
fail(int, "Not implemented!")
let data = someProc()
assert data.isOk == false
assert data.error == "Not implemented!"
test "Check changing result":
proc checker(): OP[int] =
result = ok 42
# something happend here
result = result.fail "data got corrupted"
let data = checker()
check data.isOk == false
check data.error == "data got corrupted"
test "Check divider OK":
let r = divide(12, 6)
check r.isOk == true
check r.val == 2
test "Check implicit fail type":
let r = divide(1, 0)
check r.isOk == false
check r.error == "Cannot divide by zero!"
test "Check case":
let r = divide(1, 2)
case r.isOK:
of true:
check r.val == 0.5
of false:
echo r.error
test "Check hiding of Either":
let r = divide(1, 2)
case r.isOK:
of true:
check r.val == 0.5
of false:
echo r.error
test "Expect invalid field":
let r = divide(1, 2)
expect FieldError:
echo r.error
test "Expect in case":
let r = divide(1, 2)
case r.isOK:
of true:
expect FieldError:
echo r.error
of false:
echo r.error
# These will not work. How could this be done?
# test "Expect access before check":
# let r = divide(1, 2)
# expect FieldError:
# echo r.val
# test "Expect invalid field after check":
# let r = divide(1, 2)
# if r.isOK:
# echo r.error