d = (b:10,c:"cd") echo d
I want to generated this statement based on a sequence d = (b:10,c:"cd")
What would be the best way :) can I use templates or do I need macros ?
Thanks
It is not clear what exactly you are trying to accomplish.
d = (b: 10, c: "cd")
This already creates a tuple and is not a sequence. How does the sequence you talk about look? Since the tuple needs one int and one string, you would not only need to map the sequence items to tuple fields, but also store b as string (or c as int if feasible...), because a sequence cannot contain different types.
What you want to do is probably possible with a simple generic proc and fields or fieldPairs. However:
Thanks for answers.
Sequence would have following format 10,'a' 20,'b' 30,'c'
Sorry, I will have a sequence of say object or another tuple(it can have any number of elements) In example case it has 2 field one int and another one string. I want to create a dynamic tuple based on this sequence.
My new tuple can have 1,2, or any number of fields but all of these will refer to 1 or more fields in existing sequence.
Anyway, I guess I might be able to do this one by simply assigning to tuple fields individually.
As I said, you cannot create a dynamic tuple. The tuple type must be fixed at compile time.
I believe you will get better answers if you show example code with an input sequence, and expected output. For me, it is still rather unclear what you are trying to achieve, because when you have a sequence of (int, string) tuples, you already have the tuples you want in there.
If I understand your problem right it might be a bit of confusion over how nim stores data. A big advantage of tuples is that their size is known at compile time which means that you can use them anywhere where a built in value type like an int could be used - within composite types, in the data or bss segment, the stack and so on. This however does mean that you can't create new tuple sizes at run time.
The thing all these locations have in comman is that the size of the data has to be known at compile time to use them. Anything with dynamic size has to be placed onto the heap which does a certain amount of householding to track which blocks of memory are taken or freed which in turn does impose significant runtime costs.
Long story short: If you want a dynamic length sequence of (int, string) tuples you probably want to use seq[(int, string)] or something equivalent using a seq of objects.