Guest User

Untitled

a guest
Feb 23rd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 0.58 KB | None | 0 0
  1. import options
  2.  
  3. type
  4.   Result*[T] = object
  5.     Err: T
  6.     Ok: T
  7.  
  8. var
  9.   res: Result[bool]
  10.  
  11. proc is_ok[T](): T =
  12.   if res.Ok:
  13.     return true
  14.   elif res.Err:
  15.     return false
  16.  
  17. proc is_err[T](): T =
  18.     return not is_ok()
  19.  
  20. proc ok*[T](x: T): Option[T] =
  21.   case res
  22.     of res.Ok: some(x)
  23.     of res.Err: none()
  24.  
  25. proc err*[T](x: T): Option[T] =
  26.   case res
  27.     of res.err(): some(x)
  28.     of res.ok(x): none()
  29.        
  30.  
  31. proc add(x: int, y: int): Result[string] =
  32.   if x == y:
  33.     discard ok("fine")
  34.   elif x != y:
  35.     discard err("Not Working")
  36.  
  37. echo(add(3,2))
Advertisement
Add Comment
Please, Sign In to add comment