Add Either type

This commit is contained in:
2020-07-04 16:25:29 +02:00
parent f66c5e7f18
commit 16ecac7329
2 changed files with 103 additions and 61 deletions

View File

@@ -25,28 +25,61 @@
## assert r.isOk == false
## assert r.error == "Cannot divide by zero!"
import macros
type
OP*[T] = object
## Object to wrap the result of an operation.
##
## 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
Either*[A, B] = object
case isA: bool
of true:
val*: T
a: A
of false:
error*: string
b: B
proc ok*[T](val: T): OP[T] {.inline.} =
OP*[T] = Either[T, string]
## Alias of `Either` with a string as error message
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
proc newEitherA*[A, B](a: A): Either[A, B] {.inline.} =
Either[A, B](isA: true, a: a)
proc newEitherB*[A, B](b: B): Either[A, B] {.inline.} =
Either[A, B](isA: false, b: 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)
newEitherA[T, string](val)
proc fail*(op: OP, msg: string): OP {.inline.} =
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]>`_
newEitherB[T, string](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]>`_
newEitherB[T, string](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:
@@ -56,19 +89,7 @@ proc fail*(op: OP, msg: string): OP {.inline.} =
let data = someProc()
assert data.isOk == false
assert data.error == "Not implemented!"
OP(isOK: false, error: msg)
proc fail*(T: typedesc, msg: string): OP[T] {.inline.} =
## 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](isOK: false, error: msg)
template fail*[T](O: type OP[T], msg: string): O = O(isOK: false, error: msg)
template fail*(msg: static[string]): auto = fail(typeof(result), msg)
fail(typeof(op), msg)
template fail*(msg: static[string]): auto = fail(typeof(result), msg)