mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-03 14:23:07 +00:00
Support binding closure iterators (except on Nim == 2.0)
See nim-lang/Nim#22932.
This commit is contained in:
parent
6226cbc49a
commit
24a2e4eae6
@ -2,12 +2,17 @@ import std/options
|
|||||||
import std/macros
|
import std/macros
|
||||||
import ./private/binderror
|
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] =
|
template toOption[T](option: Option[T]): Option[T] =
|
||||||
option
|
option
|
||||||
|
|
||||||
template toOption[T: ref | ptr | pointer | proc](value: T): Option[T] =
|
template toOption[T: SomePointer](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.
|
|
||||||
value.option
|
value.option
|
||||||
|
|
||||||
proc placeholder(T: type): T =
|
proc placeholder(T: type): T =
|
||||||
|
|||||||
@ -187,6 +187,18 @@ suite "optionals":
|
|||||||
if a =? p:
|
if a =? p:
|
||||||
fail
|
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":
|
test "=? rejects non-reference types":
|
||||||
check `not` compiles do:
|
check `not` compiles do:
|
||||||
if a =? 0:
|
if a =? 0:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user