summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorhlaaftana <10591326+hlaaftana@users.noreply.github.com>2020-02-22 01:14:55 +0300
committerGitHub <noreply@github.com>2020-02-21 23:14:55 +0100
commit1276e386fe68ab2ac359657297a1a2c570d7aefb (patch)
tree6d7906e5ed4d73f3b1c4ec6c54c2c98f6b33942d
parent0d219d2c67cdf123fbc38aade3974edef5ee9f8b (diff)
downloadNim-1276e386fe68ab2ac359657297a1a2c570d7aefb.tar.gz
Consider proc as a pointer type in options (#13460)
* Consider proc as a pointer type in options

* Add version annotation for SomePointer having proc in options

* Log procs as pointers for options in changelog
-rw-r--r--changelog.md2
-rw-r--r--lib/pure/options.nim15
2 files changed, 14 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md
index ce47249a8..0e0b6ce3d 100644
--- a/changelog.md
+++ b/changelog.md
@@ -25,6 +25,8 @@
   It didn't work well together with the existing inplace version of the same proc
   (`tables.merge(var CountTable, CountTable)`).
   It was an oversight to be included in v1.0.
+- `options` now treats `proc` like other pointer types, meaning `nil` proc variables
+  are converted to `None`.
 
 
 ### Breaking changes in the compiler
diff --git a/lib/pure/options.nim b/lib/pure/options.nim
index 28c4e4984..dc5cfa4bf 100644
--- a/lib/pure/options.nim
+++ b/lib/pure/options.nim
@@ -58,8 +58,12 @@
 
 import typetraits
 
-type
-  SomePointer = ref | ptr | pointer
+when (NimMajor, NimMinor) >= (1, 1):
+  type
+    SomePointer = ref | ptr | pointer | proc
+else:
+  type
+    SomePointer = ref | ptr | pointer
 
 type
   Option*[T] = object
@@ -74,7 +78,7 @@ type
 
 
 proc option*[T](val: T): Option[T] =
-  ## Can be used to convert a pointer type (`ptr` or `ref`) to an option type.
+  ## Can be used to convert a pointer type (`ptr` or `ref` or `proc`) to an option type.
   ## It converts `nil` to `None`.
   ##
   ## See also:
@@ -482,6 +486,11 @@ when isMainModule:
 
       let tmp = option(intref)
       check(sizeof(tmp) == sizeof(ptr int))
+      
+      var prc = proc (x: int): int = x + 1
+      check(option(prc).isSome)
+      prc = nil
+      check(option(prc).isNone)
 
     test "none[T]":
       check(none[int]().isNone)