results: include error when expecting (#76)

This commit is contained in:
Jacek Sieka 2021-02-10 11:05:29 +01:00 committed by GitHub
parent 6d3e6a21ca
commit a0e8ec451e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 3 deletions

View File

@ -536,10 +536,10 @@ func `$`*(self: Result): string =
func error*[T, E](self: Result[T, E]): E =
## Fetch error of result if set, or raise Defect
if self.o:
when T is not void:
when T isnot void:
raiseResultDefect("Trying to access error when value is set", self.v)
else:
raise (ref ResultDefect)(msg: "Trying to access error when value is set")
raiseResultDefect("Trying to access error when value is set")
self.e
template value*[T, E](self: Result[T, E]): T =
@ -628,7 +628,10 @@ template unsafeGet*[E](self: Result[void, E]) =
func expect*[E](self: Result[void, E], msg: string) =
if not self.o:
raise (ref ResultDefect)(msg: msg)
when E isnot void:
raiseResultDefect(msg, self.e)
else:
raiseResultDefect(msg)
func `$`*[E](self: Result[void, E]): string =
## Returns string representation of `self`