[Second review] SE-0453: Vector, a fixed size array

I do not favor Vector for a number of reasons.

"Array" and "Vector" are swapped in many languages we care about

This has been discussed to death in the context of C++ interop, so I won't belabor it too much. I will simply add that it's not only C++ that has this problem—it's also Rust (which, I think it's fair to say, is a language we're jockeying for space with) and Java (which also has an active interop project—though with the caveat that, like many of Java's early collection classes, Vector is underused because of its synchronization overhead).

While there have certainly been languages that used Vector for a general-purpose (i.e. non-SIMD) fixed-size array type, I don't think any of them are particularly relevant to Swift. All the ones which are most relevant either don't use the name "vector" for a general-purpose array type, or use it for a variable-sized one.

"Vector" sounds like geometry to most people

Much has been made of the similarities between the Vector type and mathematical vectors, but that cuts both ways. In the math education systems I'm familiar with, vectors are first introduced in the form of Euclidean vectors when studying geometry, and only during the study of linear algebra are they generalized into something resembling the Vector type being proposed here. To anyone who has studied the first concept but not the second, the name Vector is not helpful—it is actively confusing.

Now, most people who have a CS, math, or other STEM degree will probably study linear algebra, and thus at least be exposed to this more general concept of a vector. But most people period study the kind of basic geometry that involves Euclidean vectors. In the US, 92% of high school graduates have completed a geometry class. Given that only 61% of US high school graduates even enroll in a college—let alone complete enough high-level math courses to get to linear algebra—it's likely that most people will only have been exposed to the geometric version of a vector.

(And even those who have been exposed to the concept have not necessarily internalized it. I have a CS degree and studied some linear algebra in the process of getting it, but a decade of UI programming with Apple frameworks hammered in the geometric meaning of "vector" enough that the use of Vector proposed here seems distinctly odd. In conversation with my father—a retired programmer with a CS degree and a career spent in database-heavy business programming—he also said that "vector" felt like a geometric term to him; he was familiar with languages that used it for an array type but felt it confusing there.)

I'm not comfortable privileging highly-educated specialists over generalists in the naming of a currency type. We must be careful to consider not only the needs of stereotypical tech company engineers and STEM students, but also those of secondary students, non-STEM tertiary students, adult learners, engineers with nontraditional backgrounds, etc. Swift aspires to be an accessible language—let's pick a name that's more broadly understandable.

The "Array means resizing, therefore this isn't an array" argument begs the question

I've seen statements like this a few times:

These make it sound like there is some pre-existing concept of an "array" that excludes vectors. But unless I'm mistaken, this definition of a Swift "array" as requiring resizability has never been presented or established before this proposal. And when I look outside of Swift at programming languages generally, the criteria of an "array" seem to be much broader, requiring only¹:

  • Indexing by a continuous range of integers
  • Arbitrary positioning of elements (unlike e.g. a set that controls the position of each element)
  • Iteration in position order
  • O(1) access and replacement of elements in the common case
  • Homogeneous element type, to the extent the language's type system supports the concept

¹ There are languages where arrays don't necessarily always meet these criteria—usually dynamic languages which are trying to implement arrays using a dictionary-like object and which therefore struggle to keep indices contiguous or to iterate in position order—but these deviations seem to usually be regarded as quirks, pitfalls, or even bugs, not desirable features.

In particular, there are many languages—most notably C and many of its derivatives, but also including other influential languages like Fortran and Pascal—where the built-in "array" type is fixed-size.

So when you say that arrays are variable-sized by definition, you are using a definition that has no precedent in either Swift or programming languages generally. It's something new that is being established during this proposal.

That is not to say that this definition is bad or wrong—indeed, I can see how it might be useful to establish a rule that everything called an "array" is variable-sized—but that arguing as though this is a limitation we must conform to is overstating the case. The definition of Swift array types as resizable is itself under review here.

So where does that leave me?

Although Vector is not so bad that I'm willing to die on the hill of stopping it, I would prefer a different name. I am not married to a specific alternative; here are my thoughts on a number of them:

  • FixedArray or another short compound name with Array: If we want to reject the proposed array definition, then a short compound name with Array at the end seems ideal.
    • Arguments along the lines of "that means regular arrays are broken!" feel a bit like sophistry to me—pithy names are always ambiguous; they just need to be sufficiently evocative and memorable.
    • Note that the integer in the generic parameter list will also help clarify what exactly is "fixed" about a fixed array.
  • FixedSizeArray or other long compound names: These are getting too wordy. A complete description is not a name.
  • Other single-word names for groups: None of these will be immediately obvious since they're new coinages, but they're all learnable and many read well enough once you've learned them.
    • Of the ones mentioned earlier, my favorite is Slab—it's a word already associated with memory allocations and the connotations of rigidity and flatness are appropriate for a fixed-size one-dimensional array.
  • Standalone adjectives/verbs (Multiple, Repeat) or compounds with Of: I don't think we want to establish these as styles we would use for currency types.

I also have two (I believe) new suggestions:

  • Series: This is meant in the colloquial sense of "TV series", "championship series", or the "series" of a chart (several related things kept in order), rather than the mathematical sense of an infinite sum.
  • SpanStorage or similar: Conceptually similar to FixedArray, but using Span rather than Array as the point of comparison.
29 Likes