I wish to define a range type that includes all Integers except Zero. How do I do it? My current attempt looks something like this(it doesnt compile):
type
NegativeNumbers = range[int.low .. -1]
NonZeroNumber = range[NegativeNumbers(int.low) + Positive(int.high)]
Can anyone help with ideas on how to do it?Well if you really want it, no one can stop you from overloading default procedures with custom logic. E.g. :
import std/sugar
type
CustomNonRange = enum
Zero
NonZero
proc `in`(n: int, range: CustomNonRange): bool =
case range
of Zero:
n == 0
else:
n != 0
iterator items(range: CustomNonRange): int =
case range
of Zero:
yield 0
else:
for i in int.low .. int.high:
if i == 0: continue
yield i
dump 1 in NonZero # true
dump 0 in Zero # true
dump 0 in NonZero # false
dump 1 in Zero # false
for i in Zero:
echo i
for i in NonZero:
echo i # will take very long time!
break
Disclaimer: It's probably not a good idea.