I need a data structure, which is nested deeply:
["a", "b", ["c", "d", ["e"]], "f"]
I want to append items to it, I want to change its structure at runtime, how to define such a structure in Nim? how to operate deeply inside this sequence?
Doing that type safe in a statically typed language like Nim is nearly impossible.
In Nim containers like arrays and seqs have a well defined element type, but for your example you want types string and array of string at the same time.
Of course you can have a matrix, which is a seq which contains a seq for each element. But you can not have arbitrary types as elements.
What you want is available in fully dynamic languages like Ruby or Python, but I think it is rarely needed and confusing.
If you explain in more detail what you intent, then maybe someone can give you better advice.
For mixed types we have sum types in Nim, called object variants, but I guess they will not help you for above use case.
Or you may use inheritance and make all your types subtypes of root ref (see maratsims post below)
There also is https://github.com/yglukhov/variant
I think we had similar questions already, see maratsim's explanation in this tread:
Previous work was done in dynamic languages and I want to try Nim with it. http://repo.cirru.org/cirru-parser/
But I don't think it's impossible. Maybe I asked in a wrong way, I think it's solvable since there's already an example. How to you parse JSON, which is so dynamic?
Yes, I forgot to mention JSON, but it was mentioned in the thread I pointed you to,
https://forum.nim-lang.org/t/4233
I have no experience with JSON, my feeling is that you will loose most of the advantages of a compiled statically typed language, which is maximal performance and real type safely. I hope some one of the bright Nim devs will answer you soon.
what might work is if you have a
seq[tuple[elemType: EnumOfElem, ref: refToData]]
then cast it based on the elemType. But not tested and might have other issues i'm not aware of right now.