Tuples vs Arrays – can everything be done also in Arrays?

I tried a,b,c=(1,2,3) but d,e,f=[4 5 6] is also working. Is there something which work only in Tuples?

1 Like

Tuples are immutable. Arrays are mutable. That’s a night-and-day distinction. I’d encourage you to spend some time working with mutable and immutable types to gain a deeper appreciation of how many implications that distinction has. For example, major topics in programming like shared-memory parallelism depend upon understanding that distinction.

3 Likes

On the other hand: Tuples are iterable collections. Arrays are iterable collections. Anything that supports iterable collections will work with both. :slight_smile:

But as John says, the distinction between these two structures is fundamental and important to understand. In general, you’ll frequently find that the operations supported by tuples will also be supported by arrays. The converse is definitely not true.

2 Likes

Tuples are more restrictive (they’re immutable), but they’re faster. That’s the usual tradeoff.

1 Like

One can also use tuples as type parameters (as long as the elements are also valid type parameters). This can be handy for structuring dispatch/optimizations.

Also, even if a tuple contains elements of different types, each type is known at compile time. This should allow dealing with mixed-type tuples in a type-stable manner, though I’m not sure how/if type-stable iteration over a tuple is implemented or even possible.

1 Like