diff --git a/questionable/binding.nim b/questionable/binding.nim index 8d8c311..a375fc8 100644 --- a/questionable/binding.nim +++ b/questionable/binding.nim @@ -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 = diff --git a/testmodules/options/test.nim b/testmodules/options/test.nim index 98b7119..3293e42 100644 --- a/testmodules/options/test.nim +++ b/testmodules/options/test.nim @@ -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: