For example
let x: 1..2 = 1
doAssert x == 1 # ok
doAssert (x,) == (1,) # Error: type mismatch
One solution would be to cast the range to int when comparing:
doAssert (x.int,) == (1,)
Anothor kinda hacky solution would be to define a generic proc that tries to compare any tuples of same length:
import std/[macros, genasts, typetraits]
macro unroll*(forLoop: ForLoopStmt): untyped =
var forLoop = forLoop
forLoop[^2] = forLoop[^2][1]
var genAstCall = newCall(bindSym"genAst")
for forVar in forLoop[0 ..< ^2]:
if forVar.kind == nnkVarTuple:
for forVar in forVar[0 ..< ^1]:
genAstCall.add forVar
else:
genAstCall.add forVar
genAstCall.add newBlockStmt(forLoop[^1])
forLoop[^1] =
genAst(result = ident"result", genAstCall):
result.add genAstCall
result = genAst(result = ident"result", forLoop, impl = genSym(nskMacro, "impl")):
macro impl: untyped =
result = newStmtList()
forLoop
impl()
func `==`(a, b: tuple): bool =
when tupleLen(a) == tupleLen(b):
for i in unroll(0 ..< tupleLen(a)):
if a[i] != b[i]: return false
true
else:
static: error "can't compare tuples of different length"
but this makes some errors a bit less readable
(you can leave the unroll macro if you install+import my unroll lib https://github.com/choltreppe/unroll)
Or you can specify the type for (1, 1):
let x: 1..2 = 1
let expected: (range[1..2]) = (1)
let expected2: (range[1..2], int) = (1, 1)
doAssert x == 1
doAssert (x) == expected
doAssert (x, 1) == expected2
echo typeof(expected) # range 1..2(int)
echo typeof(expected2) # (range 1..2(int), int)