r/ProgrammingLanguages • u/ella-hoeppner • Apr 15 '26
Why not treat arrays as a special case of tuples?
A while ago in rust I was working on some graphics stuff, and I decided to represent positions in 2d space as a tuple of two floats, (f64, f64). But then I wanted to use a library for some geometry stuff, and tragically, I found that it had chosen to use a slightly different type to represent it's points: [f64; 2], an array of two floats.
This was slightly annoying because it meant I had to insert a bunch of conversion functions when working with the library, but so be it, that kind of thing happens a lot when trying to make different libraries work together. But it got me thinking; why should these even be considered to be different types? A tuple of two floats and an array of two floats are both just ways of grouping two floats together in a way where they can later be differentiated by an index 0 or 1, so why even have two different built-in types for representing that same thing?
Obviously, in general, the main difference between tuples and arrays (and in this post I'm always referring to fixed-sized arrays, I'm not talking about "dynamic arrays" that don't include their size as part of their type) is that arrays contain only a single type of element at each of the different indices, while tuples can be heterogenous, storing different types at different indices. In this sense, tuples are more general than arrays, in the sense that for any array type you could construct a corresponding tuple type, with the same number of elements, all of the same type.
So why not make this more than an analogy, and just have your type system literally treat arrays as a special case of tuples? I don't see any downside to this. In other words, in a language like rust, why not just have an array type like [f64; 3] literally just be a type-alias for a tuple type (f64, f64, f64)?
Of course, the main thing that you can do with arrays that you can't do with tuples is index into them with a dynamic value. In rust, to get the value out of a tuple you have to use a syntax like my_tuple.0 or my_tuple.1 to get the internal values, you can't use a syntax like my_tuple[n] with some dynamically-defined n. It wouldn't usually be possible to assign a coherent type to an expression that dynamically indexes a tuple, since in general the values in a tuple aren't of the same type. But in the case where all the types in a tuple happen to be the same, a dynamic indexing expression like that absolutely could be well-typed! So rather than having a whole separate "array" type with the only difference from a tuple being that it can be dynamically indexed, you could just have a rule in the type system saying that dynamic indexing is only allowed on homogenously-typed tuples.
Do any existing languages take this approach? Are there any downsides here that I'm not thinking of? It just seems redundant and inelegant to have arrays and tuples be fundamentally different types, when they're both just fixed-sized linear collections of values. Having a nice syntax for describing array/homogenous-tuple types is definitely important, so that you can write [f64; 100] rather than the absurdly long (f64, f64, f64, ..., but ultimately it seems more elegant for this to just be shorthand for a tuple type, rather than a fully-fledged type of its own.
And just to be clear, I'm not actually suggesting that rust, specifically, should adopt this. Obviously that would break a lot of things and the rust team is not interested in making those kinds of changes at this point in the language's development. I'm just using rust syntax as an example and making a design suggestion for brand new languages, and contemplating whether I should go this route in my own language, or whether there's some downside to this that I haven't thought about.