options.nim 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2015 Nim Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ##[
  10. This module implements types which encapsulate an optional value.
  11. A value of type `Option[T]` either contains a value `x` (represented as
  12. `some(x)`) or is empty (`none(T)`).
  13. This can be useful when you have a value that can be present or not. The
  14. absence of a value is often represented by `nil`, but that is not always
  15. available, nor is it always a good solution.
  16. Basic usage
  17. ===========
  18. Let's start with an example: a procedure that finds the index of a character
  19. in a string.
  20. ]##
  21. runnableExamples:
  22. proc find(haystack: string, needle: char): Option[int] =
  23. for i, c in haystack:
  24. if c == needle:
  25. return some(i)
  26. return none(int) # This line is actually optional,
  27. # because the default is empty
  28. let found = "abc".find('c')
  29. assert found.isSome and found.get() == 2
  30. ##[
  31. The `get` operation demonstrated above returns the underlying value, or
  32. raises `UnpackDefect` if there is no value. Note that `UnpackDefect`
  33. inherits from `system.Defect` and should therefore never be caught.
  34. Instead, rely on checking if the option contains a value with the
  35. `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
  36. Pattern matching
  37. ================
  38. .. note:: This requires the [fusion](https://github.com/nim-lang/fusion) package.
  39. [fusion/matching](https://nim-lang.github.io/fusion/src/fusion/matching.html)
  40. supports pattern matching on `Option`s, with the `Some(<pattern>)` and
  41. `None()` patterns.
  42. ```nim
  43. {.experimental: "caseStmtMacros".}
  44. import fusion/matching
  45. case some(42)
  46. of Some(@a):
  47. assert a == 42
  48. of None():
  49. assert false
  50. assertMatch(some(some(none(int))), Some(Some(None())))
  51. ```
  52. ]##
  53. # xxx pending https://github.com/timotheecour/Nim/issues/376 use `runnableExamples` and `whichModule`
  54. when defined(nimHasEffectsOf):
  55. {.experimental: "strictEffects".}
  56. else:
  57. {.pragma: effectsOf.}
  58. import std/typetraits
  59. when defined(nimPreviewSlimSystem):
  60. import std/assertions
  61. when (NimMajor, NimMinor) >= (1, 1):
  62. type
  63. SomePointer = ref | ptr | pointer | proc | iterator {.closure.}
  64. else:
  65. type
  66. SomePointer = ref | ptr | pointer
  67. type
  68. Option*[T] = object
  69. ## An optional type that may or may not contain a value of type `T`.
  70. ## When `T` is a a pointer type (`ptr`, `pointer`, `ref`, `proc` or `iterator {.closure.}`),
  71. ## `none(T)` is represented as `nil`.
  72. when T is SomePointer:
  73. val: T
  74. else:
  75. val: T
  76. has: bool
  77. UnpackDefect* = object of Defect
  78. UnpackError* {.deprecated: "See corresponding Defect".} = UnpackDefect
  79. proc option*[T](val: sink T): Option[T] {.inline.} =
  80. ## Can be used to convert a pointer type (`ptr`, `pointer`, `ref` or `proc`) to an option type.
  81. ## It converts `nil` to `none(T)`. When `T` is no pointer type, this is equivalent to `some(val)`.
  82. ##
  83. ## **See also:**
  84. ## * `some proc <#some,T>`_
  85. ## * `none proc <#none,typedesc>`_
  86. runnableExamples:
  87. type
  88. Foo = ref object
  89. a: int
  90. b: string
  91. assert option[Foo](nil).isNone
  92. assert option(42).isSome
  93. when T is SomePointer:
  94. result = Option[T](val: val)
  95. else:
  96. result = Option[T](has: true, val: val)
  97. proc some*[T](val: sink T): Option[T] {.inline.} =
  98. ## Returns an `Option` that has the value `val`.
  99. ##
  100. ## **See also:**
  101. ## * `option proc <#option,T>`_
  102. ## * `none proc <#none,typedesc>`_
  103. ## * `isSome proc <#isSome,Option[T]>`_
  104. runnableExamples:
  105. let a = some("abc")
  106. assert a.isSome
  107. assert a.get == "abc"
  108. when T is SomePointer:
  109. assert not val.isNil
  110. result = Option[T](val: val)
  111. else:
  112. result = Option[T](has: true, val: val)
  113. proc none*(T: typedesc): Option[T] {.inline.} =
  114. ## Returns an `Option` for this type that has no value.
  115. ##
  116. ## **See also:**
  117. ## * `option proc <#option,T>`_
  118. ## * `some proc <#some,T>`_
  119. ## * `isNone proc <#isNone,Option[T]>`_
  120. runnableExamples:
  121. assert none(int).isNone
  122. # the default is the none type
  123. result = Option[T]()
  124. proc none*[T]: Option[T] {.inline.} =
  125. ## Alias for `none(T) <#none,typedesc>`_.
  126. none(T)
  127. proc isSome*[T](self: Option[T]): bool {.inline.} =
  128. ## Checks if an `Option` contains a value.
  129. ##
  130. ## **See also:**
  131. ## * `isNone proc <#isNone,Option[T]>`_
  132. ## * `some proc <#some,T>`_
  133. runnableExamples:
  134. assert some(42).isSome
  135. assert not none(string).isSome
  136. when T is SomePointer:
  137. not self.val.isNil
  138. else:
  139. self.has
  140. proc isNone*[T](self: Option[T]): bool {.inline.} =
  141. ## Checks if an `Option` is empty.
  142. ##
  143. ## **See also:**
  144. ## * `isSome proc <#isSome,Option[T]>`_
  145. ## * `none proc <#none,typedesc>`_
  146. runnableExamples:
  147. assert not some(42).isNone
  148. assert none(string).isNone
  149. when T is SomePointer:
  150. self.val.isNil
  151. else:
  152. not self.has
  153. proc get*[T](self: Option[T]): lent T {.inline.} =
  154. ## Returns the content of an `Option`. If it has no value,
  155. ## an `UnpackDefect` exception is raised.
  156. ##
  157. ## **See also:**
  158. ## * `get proc <#get,Option[T],T>`_ with a default return value
  159. runnableExamples:
  160. assert some(42).get == 42
  161. doAssertRaises(UnpackDefect):
  162. echo none(string).get
  163. if self.isNone:
  164. raise newException(UnpackDefect, "Can't obtain a value from a `none`")
  165. result = self.val
  166. proc get*[T](self: Option[T], otherwise: T): T {.inline.} =
  167. ## Returns the content of the `Option` or `otherwise` if
  168. ## the `Option` has no value.
  169. runnableExamples:
  170. assert some(42).get(9999) == 42
  171. assert none(int).get(9999) == 9999
  172. if self.isSome:
  173. self.val
  174. else:
  175. otherwise
  176. proc get*[T](self: var Option[T]): var T {.inline.} =
  177. ## Returns the content of the `var Option` mutably. If it has no value,
  178. ## an `UnpackDefect` exception is raised.
  179. runnableExamples:
  180. var
  181. a = some(42)
  182. b = none(string)
  183. inc(a.get)
  184. assert a.get == 43
  185. doAssertRaises(UnpackDefect):
  186. echo b.get
  187. if self.isNone:
  188. raise newException(UnpackDefect, "Can't obtain a value from a `none`")
  189. return self.val
  190. proc map*[T](self: Option[T], callback: proc (input: T)) {.inline, effectsOf: callback.} =
  191. ## Applies a `callback` function to the value of the `Option`, if it has one.
  192. ##
  193. ## **See also:**
  194. ## * `map proc <#map,Option[T],proc(T)_2>`_ for a version with a callback
  195. ## which returns a value
  196. runnableExamples:
  197. var d = 0
  198. proc saveDouble(x: int) =
  199. d = 2 * x
  200. none(int).map(saveDouble)
  201. assert d == 0
  202. some(42).map(saveDouble)
  203. assert d == 84
  204. if self.isSome:
  205. callback(self.val)
  206. proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] {.inline, effectsOf: callback.} =
  207. ## Applies a `callback` function to the value of the `Option` and returns an
  208. ## `Option` containing the new value.
  209. ##
  210. ## If the `Option` has no value, `none(R)` will be returned.
  211. ##
  212. ## **See also:**
  213. ## * `map proc <#map,Option[T],proc(T)>`_
  214. ## * `flatMap proc <#flatMap,Option[T],proc(T)>`_ for a version with a
  215. ## callback that returns an `Option`
  216. runnableExamples:
  217. proc isEven(x: int): bool =
  218. x mod 2 == 0
  219. assert some(42).map(isEven) == some(true)
  220. assert none(int).map(isEven) == none(bool)
  221. if self.isSome:
  222. some[R](callback(self.val))
  223. else:
  224. none(R)
  225. proc flatten*[T](self: Option[Option[T]]): Option[T] {.inline.} =
  226. ## Remove one level of structure in a nested `Option`.
  227. ##
  228. ## **See also:**
  229. ## * `flatMap proc <#flatMap,Option[T],proc(T)>`_
  230. runnableExamples:
  231. assert flatten(some(some(42))) == some(42)
  232. assert flatten(none(Option[int])) == none(int)
  233. if self.isSome:
  234. self.val
  235. else:
  236. none(T)
  237. proc flatMap*[T, R](self: Option[T],
  238. callback: proc (input: T): Option[R]): Option[R] {.inline, effectsOf: callback.} =
  239. ## Applies a `callback` function to the value of the `Option` and returns the new value.
  240. ##
  241. ## If the `Option` has no value, `none(R)` will be returned.
  242. ##
  243. ## This is similar to `map`, with the difference that the `callback` returns an
  244. ## `Option`, not a raw value. This allows multiple procs with a
  245. ## signature of `A -> Option[B]` to be chained together.
  246. ##
  247. ## See also:
  248. ## * `flatten proc <#flatten,Option[Option[A]]>`_
  249. ## * `filter proc <#filter,Option[T],proc(T)>`_
  250. runnableExamples:
  251. proc doublePositives(x: int): Option[int] =
  252. if x > 0:
  253. some(2 * x)
  254. else:
  255. none(int)
  256. assert some(42).flatMap(doublePositives) == some(84)
  257. assert none(int).flatMap(doublePositives) == none(int)
  258. assert some(-11).flatMap(doublePositives) == none(int)
  259. map(self, callback).flatten()
  260. proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline, effectsOf: callback.} =
  261. ## Applies a `callback` to the value of the `Option`.
  262. ##
  263. ## If the `callback` returns `true`, the option is returned as `some`.
  264. ## If it returns `false`, it is returned as `none`.
  265. ##
  266. ## **See also:**
  267. ## * `flatMap proc <#flatMap,Option[A],proc(A)>`_
  268. runnableExamples:
  269. proc isEven(x: int): bool =
  270. x mod 2 == 0
  271. assert some(42).filter(isEven) == some(42)
  272. assert none(int).filter(isEven) == none(int)
  273. assert some(-11).filter(isEven) == none(int)
  274. if self.isSome and not callback(self.val):
  275. none(T)
  276. else:
  277. self
  278. proc `==`*[T](a, b: Option[T]): bool {.inline.} =
  279. ## Returns `true` if both `Option`s are `none`,
  280. ## or if they are both `some` and have equal values.
  281. runnableExamples:
  282. let
  283. a = some(42)
  284. b = none(int)
  285. c = some(42)
  286. d = none(int)
  287. assert a == c
  288. assert b == d
  289. assert not (a == b)
  290. when T is SomePointer:
  291. a.val == b.val
  292. else:
  293. (a.isSome and b.isSome and a.val == b.val) or (a.isNone and b.isNone)
  294. proc `$`*[T](self: Option[T]): string =
  295. ## Get the string representation of the `Option`.
  296. runnableExamples:
  297. assert $some(42) == "some(42)"
  298. assert $none(int) == "none(int)"
  299. if self.isSome:
  300. when defined(nimLagacyOptionsDollar):
  301. result = "Some("
  302. else:
  303. result = "some("
  304. result.addQuoted self.val
  305. result.add ")"
  306. else:
  307. when defined(nimLagacyOptionsDollar):
  308. result = "None[" & name(T) & "]"
  309. else:
  310. result = "none(" & name(T) & ")"
  311. proc unsafeGet*[T](self: Option[T]): lent T {.inline.}=
  312. ## Returns the value of a `some`. The behavior is undefined for `none`.
  313. ##
  314. ## **Note:** Use this only when you are **absolutely sure** the value is present
  315. ## (e.g. after checking with `isSome <#isSome,Option[T]>`_).
  316. ## Generally, using the `get proc <#get,Option[T]>`_ is preferred.
  317. assert self.isSome
  318. result = self.val