Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider proc as a pointer type in options #13460

Merged
merged 3 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions lib/pure/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down