is there any way to write..
nim
if a == b == c == d:
....
else:
...
if a == b and b == c and c == d:
echo "ok"
else:
echo "bad"
Rough macro to emulate this behavior:
import macros
macro chain(ex): untyped =
var values, ops: seq[NimNode]
var last = ex
while last.kind == nnkInfix:
# assuming left associativity
ops.insert(last[0], 0)
values.insert(last[2], 0)
last = last[1]
values.insert(last, 0)
var exprs: seq[NimNode]
var valueIndex = 0
for o in ops:
exprs.add(newTree(nnkInfix, o, values[valueIndex], values[valueIndex + 1]))
inc valueIndex
result = exprs[0]
for i in 1 ..< exprs.len:
result = infix(result, "and", exprs[i])
echo result.repr
echo chain(1 == 1 == 1) # => 1 == 1 and 1 == 1
echo chain(1 < 2 < 3 > 0) # => 1 < 2 and 2 < 3 and 3 > 0
BTW, what is a Nim-way of a < b < c statement for float vars? The shortest way I found is:
b in a .. c
but maybe there is something proper?