From eea8cf425084d4e8a2fdc85de8e8e8a67be534ec Mon Sep 17 00:00:00 2001 From: Dan Rose Date: Wed, 12 Jul 2023 13:03:37 -0500 Subject: [PATCH] Fix tuple concatenation containing lists, tuples, refs Bump to 0.5.3 --- records.nimble | 2 +- src/records/tupleops.nim | 8 ++++---- tests/testTuple.nim | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/records.nimble b/records.nimble index e67191a..11c60a1 100644 --- a/records.nimble +++ b/records.nimble @@ -1,7 +1,7 @@ import os # Package -version = "0.5.2" +version = "0.5.3" author = "Dan Rose" description = "Operations on tuples as heterogeneous record types a la Relational Algebra" license = "MIT" diff --git a/src/records/tupleops.nim b/src/records/tupleops.nim index 6c44ca6..de95e4d 100644 --- a/src/records/tupleops.nim +++ b/src/records/tupleops.nim @@ -13,9 +13,6 @@ proc concat*(t1: tuple, t2: tuple): tuple = expectKind(arg.getTypeImpl(), {nnkTupleConstr, nnkTupleTy}) for i, d in pairs(arg.getTypeImpl): case kind(d): - of nnkSym: - # un-named tuple field - result.add newTree(nnkBracketExpr, arg, newLit(i)) of nnkIdentDefs: # named tuple field let prop = d[0] @@ -23,7 +20,10 @@ proc concat*(t1: tuple, t2: tuple): tuple = newTree(nnkDotExpr, arg, prop) ) else: - error("Unexpected field kind: `" & $kind(d) & "`") + # un-named tuple field + # usually d is of kind nnkSym + # but can be nnkBracketExpr, nnkTupleConstr, nnkRefTy + result.add newTree(nnkBracketExpr, arg, newLit(i)) concatImpl() proc `&` *(t1: tuple, t2: tuple): auto = concat(t1, t2) diff --git a/tests/testTuple.nim b/tests/testTuple.nim index e8b07e3..3c3cfdf 100644 --- a/tests/testTuple.nim +++ b/tests/testTuple.nim @@ -45,6 +45,36 @@ test "concatenation": let c = concat( (1, 2), (3, "a")) check c == (1, 2, 3, "a") + check concat((),()) == () + check concat((),(1,)) == (1,) + check concat((1,),()) == (1,) + +test "concatenateWithLists": + let a = ([1],[2,3]) + let b = ([4],) + check a & b == ([1],[2,3],[4]) + + let d = (x:[1],y:[2,3]) + let e = (z:[4],) + check d & e == (x:[1],y:[2,3],z:[4]) + +test "concatenateWithTuples": + let a = ((),) + let b = ((1,2,3),(4)) + check a & b == ((),(1,2,3),(4)) + +test "concatenateWithObjects": + type Foo = object + x:int + let a = Foo(x:1) + let b = Foo(x:2) + check concat((a,),(b,)) == (a,b) + check concat((a,),(b,)) != (b,a) + + let ra = (ref Foo)(x:1) + + check concat((ra,),(a,)) == (ra,a) + test "join": let x = (a: 1, b: 2) check join(x, x) == some(x)