Apologies, if it is a too simple question.
Is it possible define a tuple of fixed length arrays such that every array could store a different type, but have the same length. Enforce at compile that all fixed length arrays were of the same length.
Structure that replicates column-oriented SQL table.
Many thanks to the authors of the language, I am impressed.
Since arrays need to have a know length at compilè time, a tuple of arrays is simply:
type
A = tuple
ints:array[3,int] # an array that contains 3 integers
strings:array[3,string] # 3 strings
var
arrint = [1,2,3]
arrstr = ["one","two","three"]
a : A = (ints:arrint, strings:arrstr)
echo a.ints[0] # 1
echo a.strings[0] # one
Is this what you meant? Obviusly you can't change the array lenght at runtime, but you can overwrite elements in the arrays. Also, is there a reason you specifically want a tuple and not an object?Here:
type MyTable[T: static[int]] = tuple[ints: array[T, int], strings: array[T, string]]
var t: MyTable[3] = (ints: [1, 2, 3], strings: ["one", "two", "three"])
Thanks a lot!
MyTable[T: static[int]]
was the missing part of the puzzle.