Static Arrays

Currently, if one wants to write a compound type of n values of type T, one must write the type T n times in the tuple notation, which is extremely tedious and cumbersome as n becomes arbitrarily large, plus it has the added disadvantage of not being subscriptable or strideable as well as being hard to read the more values there are. In order to prevent to fix this, I propose that a feature like static arrays be added to Swift. Such a feature would also make it easier to interface with (Objective-)C code, as it would make using (Objective-)C static arrays easier. Currently, in Objective-C they're bridged to n-tuples with repeated types, which are a lot harder to work with as you must count up the number of values, which can be a lot. Also, static arrays already have precedence in other programming languages like C, C++, Objective-C, and Rust. I suggest using syntax closer to the Rust syntax for static arrays i.e. [T; n] (where T is a type and n is an integer literal) as it looks the most "Swifty" and it would extend the already existing array syntax.

2 Likes

I agree that Swift could use fixed-size arrays. In the mean time, you can create your own types. This thread has some suggestions on how to do that: Fixed size array hacks

If you want the member access that you get from tuples (tuple.0, tuple.1, etc.) and basically get type-safe indexing, then you can make your type @dynamicMemberAccessible, where the key-path taken in by the subscript is KeyPath<Storage, Element>.

1 Like