options.nim 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. ## This module implements types which encapsulate an optional value.
  10. ##
  11. ## A value of type `Option[T]` either contains a value `x` (represented as
  12. ## `some(x)`) or is empty (`none(T)`).
  13. ##
  14. ## This can be useful when you have a value that can be present or not. The
  15. ## absence of a value is often represented by `nil`, but it is not always
  16. ## available, nor is it always a good solution.
  17. ##
  18. ##
  19. ## Basic usage
  20. ## ===========
  21. ##
  22. ## Let's start with an example: a procedure that finds the index of a character
  23. ## in a string.
  24. ##
  25. ## .. code-block:: nim
  26. ##
  27. ## import options
  28. ##
  29. ## proc find(haystack: string, needle: char): Option[int] =
  30. ## for i, c in haystack:
  31. ## if c == needle:
  32. ## return some(i)
  33. ## return none(int) # This line is actually optional,
  34. ## # because the default is empty
  35. ##
  36. ## .. code-block:: nim
  37. ##
  38. ## let found = "abc".find('c')
  39. ## assert found.isSome and found.get() == 2
  40. ##
  41. ## The `get` operation demonstrated above returns the underlying value, or
  42. ## raises `UnpackDefect` if there is no value. Note that `UnpackDefect`
  43. ## inherits from `system.Defect`, and should therefore never be caught.
  44. ## Instead, rely on checking if the option contains a value with
  45. ## `isSome <#isSome,Option[T]>`_ and `isNone <#isNone,Option[T]>`_ procs.
  46. ##
  47. ## How to deal with an absence of a value:
  48. ##
  49. ## .. code-block:: nim
  50. ##
  51. ## let result = "team".find('i')
  52. ##
  53. ## # Nothing was found, so the result is `none`.
  54. ## assert(result == none(int))
  55. ## # It has no value:
  56. ## assert(result.isNone)
  57. import typetraits
  58. when (NimMajor, NimMinor) >= (1, 1):
  59. type
  60. SomePointer = ref | ptr | pointer | proc
  61. else:
  62. type
  63. SomePointer = ref | ptr | pointer
  64. type
  65. Option*[T] = object
  66. ## An optional type that stores its value and state separately in a boolean.
  67. when T is SomePointer:
  68. val: T
  69. else:
  70. val: T
  71. has: bool
  72. UnpackDefect* = object of Defect
  73. UnpackError* {.deprecated: "See corresponding Defect".} = UnpackDefect
  74. proc option*[T](val: T): Option[T] {.inline.} =
  75. ## Can be used to convert a pointer type (`ptr` or `ref` or `proc`) to an option type.
  76. ## It converts `nil` to `None`.
  77. ##
  78. ## See also:
  79. ## * `some <#some,T>`_
  80. ## * `none <#none,typedesc>`_
  81. runnableExamples:
  82. type
  83. Foo = ref object
  84. a: int
  85. b: string
  86. var c: Foo
  87. assert c.isNil
  88. var d = option(c)
  89. assert d.isNone
  90. result.val = val
  91. when T isnot SomePointer:
  92. result.has = true
  93. proc some*[T](val: T): Option[T] {.inline.} =
  94. ## Returns an `Option` that has the value `val`.
  95. ##
  96. ## See also:
  97. ## * `option <#option,T>`_
  98. ## * `none <#none,typedesc>`_
  99. ## * `isSome <#isSome,Option[T]>`_
  100. runnableExamples:
  101. var
  102. a = some("abc")
  103. b = some(42)
  104. assert $type(a) == "Option[system.string]"
  105. assert b.isSome
  106. assert a.get == "abc"
  107. assert $b == "Some(42)"
  108. when T is SomePointer:
  109. assert(not val.isNil)
  110. result.val = val
  111. else:
  112. result.has = true
  113. result.val = val
  114. proc none*(T: typedesc): Option[T] {.inline.} =
  115. ## Returns an `Option` for this type that has no value.
  116. ##
  117. ## See also:
  118. ## * `option <#option,T>`_
  119. ## * `some <#some,T>`_
  120. ## * `isNone <#isNone,Option[T]>`_
  121. runnableExamples:
  122. var a = none(int)
  123. assert a.isNone
  124. assert $type(a) == "Option[system.int]"
  125. # the default is the none type
  126. discard
  127. proc none*[T]: Option[T] {.inline.} =
  128. ## Alias for `none(T) proc <#none,typedesc>`_.
  129. none(T)
  130. proc isSome*[T](self: Option[T]): bool {.inline.} =
  131. ## Checks if an `Option` contains a value.
  132. runnableExamples:
  133. var
  134. a = some(42)
  135. b = none(string)
  136. assert a.isSome
  137. assert not b.isSome
  138. when T is SomePointer:
  139. not self.val.isNil
  140. else:
  141. self.has
  142. proc isNone*[T](self: Option[T]): bool {.inline.} =
  143. ## Checks if an `Option` is empty.
  144. runnableExamples:
  145. var
  146. a = some(42)
  147. b = none(string)
  148. assert not a.isNone
  149. assert b.isNone
  150. when T is SomePointer:
  151. self.val.isNil
  152. else:
  153. not self.has
  154. proc get*[T](self: Option[T]): lent T {.inline.} =
  155. ## Returns contents of an `Option`. If it is `None`, then an exception is
  156. ## thrown.
  157. ##
  158. ## See also:
  159. ## * `get proc <#get,Option[T],T>`_ with the default return value
  160. runnableExamples:
  161. let
  162. a = some(42)
  163. b = none(string)
  164. assert a.get == 42
  165. doAssertRaises(UnpackDefect):
  166. echo b.get
  167. if self.isNone:
  168. raise newException(UnpackDefect, "Can't obtain a value from a `none`")
  169. result = self.val
  170. proc get*[T](self: Option[T], otherwise: T): T {.inline.} =
  171. ## Returns the contents of the `Option` or an `otherwise` value if
  172. ## the `Option` is `None`.
  173. runnableExamples:
  174. var
  175. a = some(42)
  176. b = none(int)
  177. assert a.get(9999) == 42
  178. assert b.get(9999) == 9999
  179. if self.isSome:
  180. self.val
  181. else:
  182. otherwise
  183. proc get*[T](self: var Option[T]): var T {.inline.} =
  184. ## Returns contents of the `var Option`. If it is `None`, then an exception
  185. ## is thrown.
  186. runnableExamples:
  187. let
  188. a = some(42)
  189. b = none(string)
  190. assert a.get == 42
  191. doAssertRaises(UnpackDefect):
  192. echo b.get
  193. if self.isNone:
  194. raise newException(UnpackDefect, "Can't obtain a value from a `none`")
  195. return self.val
  196. proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} =
  197. ## Applies a `callback` function to the value of the `Option`, if it has one.
  198. ##
  199. ## See also:
  200. ## * `map proc <#map,Option[T],proc(T)_2>`_ for a version with a callback
  201. ## which returns a value
  202. ## * `filter proc <#filter,Option[T],proc(T)>`_
  203. runnableExamples:
  204. var d = 0
  205. proc saveDouble(x: int) =
  206. d = 2*x
  207. let
  208. a = some(42)
  209. b = none(int)
  210. b.map(saveDouble)
  211. assert d == 0
  212. a.map(saveDouble)
  213. assert d == 84
  214. if self.isSome:
  215. callback(self.val)
  216. proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] {.inline.} =
  217. ## Applies a `callback` function to the value of the `Option` and returns an
  218. ## `Option` containing the new value.
  219. ##
  220. ## If the `Option` is `None`, `None` of the return type of the `callback`
  221. ## will be returned.
  222. ##
  223. ## See also:
  224. ## * `flatMap proc <#flatMap,Option[A],proc(A)>`_ for a version with a
  225. ## callback which returns an `Option`
  226. ## * `filter proc <#filter,Option[T],proc(T)>`_
  227. runnableExamples:
  228. var
  229. a = some(42)
  230. b = none(int)
  231. proc isEven(x: int): bool =
  232. x mod 2 == 0
  233. assert $(a.map(isEven)) == "Some(true)"
  234. assert $(b.map(isEven)) == "None[bool]"
  235. if self.isSome:
  236. some[R](callback(self.val))
  237. else:
  238. none(R)
  239. proc flatten*[A](self: Option[Option[A]]): Option[A] {.inline.} =
  240. ## Remove one level of structure in a nested `Option`.
  241. runnableExamples:
  242. let a = some(some(42))
  243. assert $flatten(a) == "Some(42)"
  244. if self.isSome:
  245. self.val
  246. else:
  247. none(A)
  248. proc flatMap*[A, B](self: Option[A],
  249. callback: proc (input: A): Option[B]): Option[B] {.inline.} =
  250. ## Applies a `callback` function to the value of the `Option` and returns an
  251. ## `Option` containing the new value.
  252. ##
  253. ## If the `Option` is `None`, `None` of the return type of the `callback`
  254. ## will be returned.
  255. ##
  256. ## Similar to `map`, with the difference that the `callback` returns an
  257. ## `Option`, not a raw value. This allows multiple procs with a
  258. ## signature of `A -> Option[B]` to be chained together.
  259. ##
  260. ## See also:
  261. ## * `flatten proc <#flatten,Option[Option[A]]>`_
  262. ## * `filter proc <#filter,Option[T],proc(T)>`_
  263. runnableExamples:
  264. proc doublePositives(x: int): Option[int] =
  265. if x > 0:
  266. return some(2*x)
  267. else:
  268. return none(int)
  269. let
  270. a = some(42)
  271. b = none(int)
  272. c = some(-11)
  273. assert a.flatMap(doublePositives) == some(84)
  274. assert b.flatMap(doublePositives) == none(int)
  275. assert c.flatMap(doublePositives) == none(int)
  276. map(self, callback).flatten()
  277. proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline.} =
  278. ## Applies a `callback` to the value of the `Option`.
  279. ##
  280. ## If the `callback` returns `true`, the option is returned as `Some`.
  281. ## If it returns `false`, it is returned as `None`.
  282. ##
  283. ## See also:
  284. ## * `map proc <#map,Option[T],proc(T)_2>`_
  285. ## * `flatMap proc <#flatMap,Option[A],proc(A)>`_
  286. runnableExamples:
  287. proc isEven(x: int): bool =
  288. x mod 2 == 0
  289. let
  290. a = some(42)
  291. b = none(int)
  292. c = some(-11)
  293. assert a.filter(isEven) == some(42)
  294. assert b.filter(isEven) == none(int)
  295. assert c.filter(isEven) == none(int)
  296. if self.isSome and not callback(self.val):
  297. none(T)
  298. else:
  299. self
  300. proc `==`*(a, b: Option): bool {.inline.} =
  301. ## Returns `true` if both `Option`s are `None`,
  302. ## or if they are both `Some` and have equal values.
  303. runnableExamples:
  304. let
  305. a = some(42)
  306. b = none(int)
  307. c = some(42)
  308. d = none(int)
  309. assert a == c
  310. assert b == d
  311. assert not (a == b)
  312. (a.isSome and b.isSome and a.val == b.val) or (not a.isSome and not b.isSome)
  313. proc `$`*[T](self: Option[T]): string =
  314. ## Get the string representation of the `Option`.
  315. ##
  316. ## If the `Option` has a value, the result will be `Some(x)` where `x`
  317. ## is the string representation of the contained value.
  318. ## If the `Option` does not have a value, the result will be `None[T]`
  319. ## where `T` is the name of the type contained in the `Option`.
  320. if self.isSome:
  321. result = "Some("
  322. result.addQuoted self.val
  323. result.add ")"
  324. else:
  325. result = "None[" & name(T) & "]"
  326. proc unsafeGet*[T](self: Option[T]): lent T {.inline.}=
  327. ## Returns the value of a `some`. Behavior is undefined for `none`.
  328. ##
  329. ## **Note:** Use it only when you are **absolutely sure** the value is present
  330. ## (e.g. after checking `isSome <#isSome,Option[T]>`_).
  331. ## Generally, using `get proc <#get,Option[T]>`_ is preferred.
  332. assert self.isSome
  333. result = self.val
  334. when isMainModule:
  335. import unittest, sequtils
  336. # RefPerson is used to test that overloaded `==` operator is not called by
  337. # options. It is defined here in the global scope, because otherwise the test
  338. # will not even consider the `==` operator. Different bug?
  339. type RefPerson = ref object
  340. name: string
  341. proc `==`(a, b: RefPerson): bool =
  342. assert(not a.isNil and not b.isNil)
  343. a.name == b.name
  344. suite "options":
  345. # work around a bug in unittest
  346. let intNone = none(int)
  347. let stringNone = none(string)
  348. test "example":
  349. proc find(haystack: string, needle: char): Option[int] =
  350. for i, c in haystack:
  351. if c == needle:
  352. return some i
  353. check("abc".find('c').get() == 2)
  354. let result = "team".find('i')
  355. check result == intNone
  356. check result.isNone
  357. test "some":
  358. check some(6).get() == 6
  359. check some("a").unsafeGet() == "a"
  360. check some(6).isSome
  361. check some("a").isSome
  362. test "none":
  363. expect UnpackDefect:
  364. discard none(int).get()
  365. check(none(int).isNone)
  366. check(not none(string).isSome)
  367. test "equality":
  368. check some("a") == some("a")
  369. check some(7) != some(6)
  370. check some("a") != stringNone
  371. check intNone == intNone
  372. when compiles(some("a") == some(5)):
  373. check false
  374. when compiles(none(string) == none(int)):
  375. check false
  376. test "get with a default value":
  377. check(some("Correct").get("Wrong") == "Correct")
  378. check(stringNone.get("Correct") == "Correct")
  379. test "$":
  380. check($(some("Correct")) == "Some(\"Correct\")")
  381. check($(stringNone) == "None[string]")
  382. test "map with a void result":
  383. var procRan = 0
  384. some(123).map(proc (v: int) = procRan = v)
  385. check procRan == 123
  386. intNone.map(proc (v: int) = check false)
  387. test "map":
  388. check(some(123).map(proc (v: int): int = v * 2) == some(246))
  389. check(intNone.map(proc (v: int): int = v * 2).isNone)
  390. test "filter":
  391. check(some(123).filter(proc (v: int): bool = v == 123) == some(123))
  392. check(some(456).filter(proc (v: int): bool = v == 123).isNone)
  393. check(intNone.filter(proc (v: int): bool = check false).isNone)
  394. test "flatMap":
  395. proc addOneIfNotZero(v: int): Option[int] =
  396. if v != 0:
  397. result = some(v + 1)
  398. else:
  399. result = none(int)
  400. check(some(1).flatMap(addOneIfNotZero) == some(2))
  401. check(some(0).flatMap(addOneIfNotZero) == none(int))
  402. check(some(1).flatMap(addOneIfNotZero).flatMap(addOneIfNotZero) == some(3))
  403. proc maybeToString(v: int): Option[string] =
  404. if v != 0:
  405. result = some($v)
  406. else:
  407. result = none(string)
  408. check(some(1).flatMap(maybeToString) == some("1"))
  409. proc maybeExclaim(v: string): Option[string] =
  410. if v != "":
  411. result = some v & "!"
  412. else:
  413. result = none(string)
  414. check(some(1).flatMap(maybeToString).flatMap(maybeExclaim) == some("1!"))
  415. check(some(0).flatMap(maybeToString).flatMap(maybeExclaim) == none(string))
  416. test "SomePointer":
  417. var intref: ref int
  418. check(option(intref).isNone)
  419. intref.new
  420. check(option(intref).isSome)
  421. let tmp = option(intref)
  422. check(sizeof(tmp) == sizeof(ptr int))
  423. var prc = proc (x: int): int = x + 1
  424. check(option(prc).isSome)
  425. prc = nil
  426. check(option(prc).isNone)
  427. test "none[T]":
  428. check(none[int]().isNone)
  429. check(none(int) == none[int]())
  430. test "$ on typed with .name":
  431. type Named = object
  432. name: string
  433. let nobody = none(Named)
  434. check($nobody == "None[Named]")
  435. test "$ on type with name()":
  436. type Person = object
  437. myname: string
  438. let noperson = none(Person)
  439. check($noperson == "None[Person]")
  440. test "Ref type with overloaded `==`":
  441. let p = some(RefPerson.new())
  442. check p.isSome