Ok so. In Nim a Tuple uses the same idea as a vector in R: structural typing. Hopefully I'm on track here. Nim has the ability to do stuff during compile time as opposed to runtime, so the functions that are built in for Tuples, like ==
have a loop in their source code, but in actuality the loop is unraveling at compile time, and the fields are being referenced directly at runtime. So for speed, Tuple == Tuple will be as fast as it could possibly be, regardless of the number of fields in the Tuple. This can be verified via the source code.
I am certain you can do exactly what you are asking after verifying that this little function I wrote works since *
wasn't built in for Tuples:
let
v1 = (1,2,3,4)
v2 = (7,2,7,4)
proc `*`[T:tuple](x, y: T): T =
for a,b in fields(x, result):
b = a
for a,b in fields(y, result):
b = b * a
echo $(v1 * v2) #prints (Field0: 7, Field1: 4, Field2: 21, Field3: 16)
We could write the same function for addition:
let
v1 = (1,2,3,4)
v2 = (7,2,7,4)
proc `+`[T:tuple](x, y: T): T =
for a,b in fields(x, result):
b = a
for a,b in fields(y, result):
b = b + a
echo $(v1 + v2)
Here's a full example which is a combination of the forum convo and the functions above:
proc `+`[T:tuple](x, y: T): T =
for a,b in fields(x, result):
b = a
for a,b in fields(y, result):
b = b + a
proc `*`[T:tuple](x, y: T): T =
for a,b in fields(x, result):
b = a
for a,b in fields(y, result):
b = b * a
let
a = (1,2,3,4)
b = (7,2,7,4)
c = if v1 == v2: v1 * v2 else: v1 + v2
Hope this helps!