File tree Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Expand file tree Collapse file tree 1 file changed +15
-1
lines changed Original file line number Diff line number Diff line change 1
1
2
2
from ./ collections_abc import Iterable
3
+ import ./ noneType
4
+ import std/ options
3
5
4
6
type
5
7
PyBool* = distinct bool
@@ -55,24 +57,36 @@ converter bool*(arg: HasMoreThan): bool = arg > 0 or arg < 0
55
57
converter bool*(arg: CanCompToNil): bool = arg != nil
56
58
]#
57
59
60
+ proc optionToBool[T](arg: Option[T]): bool
61
+
58
62
template toBool*[T](arg: T): bool =
59
63
## Converts argument to boolean, checking python-like truthiness.
64
+ bind None, isSome, Option, optionToBool
60
65
# If we have len proc for this object
61
66
when compiles(arg.len):
62
67
arg.len > 0
63
68
elif T is SomeNumber:
64
- # If we can compare if it's not 0
69
+ # If we can compare if it's not 0
65
70
arg != 0 # works for NaN
66
71
# If we can compare if it's greater than 0
67
72
elif compiles(arg > 0):
68
73
arg > 0 or arg < 0
69
74
# Initialized variables only
70
75
elif compiles(arg != nil):
71
76
arg != nil
77
+ elif compiles(arg != None):
78
+ arg != None
79
+ elif compiles(bool (arg)):
80
+ bool (arg)
81
+ elif T is Option:
82
+ arg.optionToBool
72
83
else:
73
84
# XXX: is this correct?
74
85
true
75
86
87
+ proc optionToBool[T](arg: Option[T]): bool =
88
+ arg.isSome and arg.unsafeGet.toBool
89
+
76
90
converter toNimBool*(self): bool = bool (self)
77
91
converter pybool*(x: bool ): PyBool = PyBool(x)
78
92
converter pybool*[T](x: T): PyBool =
You can’t perform that action at this time.
0 commit comments