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".
Types
OP[T] = object of RootObj case isOk*: bool of true: val*: T of false: error*: string
-
Object to wrap the result of an operation
- 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
Procs
proc ok[T](val: T): OP[T]
- Wraps the given value in a successful operation result.
proc 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.
Examples:
proc someProc(): OP[int] = result.fail "Not implemented!" let data = someProc() assert data.isOk == false assert data.error == "Not implemented!"
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:
Examples:
let res = fail[seq[float]] "Something is wrong!" assert res.isOk == false assert res.error == "Something is wrong!"