Unless I've missed it, it seems like nim doesn't offer a "splat" operator for unpacking (some) collections. In Python, they use asterisks, see PEP448. In Julia, there is a ellipsis operator for "slurping" and "splatting". We need not adopt such onomatopoeic names, I'm happy to just have a civilized "unpacking" operator ;)
This thread is to gather potential interest and maybe for smarter people to outline how it would work or what the limitations would be.
There was some similar discussion here which points to a bunch of solutions that people have come up with already. I haven't checked them much yet, but they are probably worth a look. For this thread, I'm talking about having either one of these or something similar in the stdlib, probably in sequtils unless there's a better namespace.
Links for le monsieur:
There is a module in fusion for pattern matching. There have been efforts to move this to the standard library but it seems like the goal has been changed to have an implementation in the language itself as shown in the roadmap. This means that either the language will have to choose a splat operator, or there will still just be no splat operator.
As it stands though, the native unpacking syntax is weird. For some reason it supports postfix * markers (but not types) which means it is incompatible with the rest of the language's syntax and its parsing is written manually. It's represented by a nkVarTuple node that isn't used anywhere else, except in for loops which have inconsistent AST with variable unpacking. On top of that its behavior is hardcoded into the language and it only supports tuples. And there's no native way to slice tuples (user implemented way here). So a splat operator would be hard to find a place for.
Yes, I noticed that it is only for tuples :(
This is unfortunate. Performance considerations aside, concise unpacking seems like a very convenient thing to have. Maybe I'm still holding too tightly to my dynamic typing ways, but in Julia/Python I enjoy using small namedtuples and unpacking them as required. Julia splatting is mostly used in function calls, including constructors. The original motivation for this issue was similar: I wanted to unpack elements of a sequence into an array. Without unpacking, I have to do [mySeq[0], mySeq[1], mySeq[2]] which feels awkward and sucks when frequently refactoring, e.g during prototyping.
Oh well, I guess I'll look into those packages for larger projects. Hopefully some of them become available in the nimble directory at some point.
Just learned about tupleLen from std/typetraits which could make things a bit less clunky at the cost of a manual loop. However, I'm a bit tired so I can't work out how to fix this:
import typetraits
var t = (1, 2, 3)
var a:array[tupleLen(t), int]
for i in 0..tupleLen(t):
a[i] = t[i]
Would neeed to make the loop happen pre compilation, maybe a macro?