I've recently encountered this example of the splat operator:
first, *, last = 1, 2, *[3,4,5]
and it worked.
so I was wondering, can you use the splat operator more than once on either the left or the right side? I get unexpected star error each time I do that, but was just wondering for the heck of it.
You can't have more than one splat on the left side. (At least not on the same level of nesting.) After all, it means "gobble up all values", so what would two splats even mean? It just doesn't make sense.
Note: something like this is of course possible:
a, *b, (c, *d) = [1, 2, 3, [4, 5, 6]]
The two splats are on different levels of nesting, they don't interfere with each other.
And on the right hand side, there's no problem either:
a = 1, 2, *[3, 4], *[5, 6]
It just means expansion, and there's no problem expanding multiple arrays on the right hand side.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With