Skip to content

Commit

Permalink
Support binding closure iterators (except on Nim == 2.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirNickolas authored and markspanbroek committed Nov 14, 2023
1 parent fe47a19 commit cdf639c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 8 additions & 3 deletions questionable/binding.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import std/options
import std/macros
import ./private/binderror

when (NimMajor, NimMinor) < (1, 1):
type SomePointer = ref | ptr | pointer
elif (NimMajor, NimMinor) == (2, 0): # Broken in 2.0.0, fixed in 2.1.1.
type SomePointer = ref | ptr | pointer | proc
else:
type SomePointer = ref | ptr | pointer | proc | iterator {.closure.}

template toOption[T](option: Option[T]): Option[T] =
option

template toOption[T: ref | ptr | pointer | proc](value: T): Option[T] =
# `std/options` don't consider closure iterators to be pointer types
# (probably a bug) so we don't list them here.
template toOption[T: SomePointer](value: T): Option[T] =
value.option

proc placeholder(T: type): T =
Expand Down
12 changes: 12 additions & 0 deletions testmodules/options/test.nim
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ suite "optionals":
if a =? p:
fail

when (NimMajor, NimMinor) >= (1, 1) and (NimMajor, NimMinor) != (2, 0):
var it = iterator: int = yield 2
if a =? it:
for x in a:
check x == 2
else:
fail

it = nil
if a =? it:
fail

test "=? rejects non-reference types":
check `not` compiles do:
if a =? 0:
Expand Down

0 comments on commit cdf639c

Please sign in to comment.