Skip to content

Commit 02a4c02

Browse files
InlineArray sugar proposal (#2776)
* First draft of InlineArray sugar proposal * Add flattened array future direction * Add alternative of of * flesh out alternatives considered * Clarify x is a keyword * typo * Assign `InlineArray` type sugar to SE-0483 and put into review. * Fix a missing link to SE-0453. --------- Co-authored-by: Holly Borla <[email protected]>
1 parent 142e5dd commit 02a4c02

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed

proposals/0483-inline-array-sugar.md

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# `InlineArray` Literal Syntax
2+
3+
* Proposal: [SE-0483](0483-inline-array-sugar.md)
4+
* Authors: [Hamish Knight](https://github.com/hamishknight), [Ben Cohen](https://github.com/airspeedswift)
5+
* Review Manager: [Holly Borla](https://github.com/hborla)
6+
* Status: **Active Review (May 2 - May 16, 2025)**
7+
* Implementation: On `main` under the `InlineArrayTypeSugar` experimental feature flag.
8+
* Review: ([pitch](https://forums.swift.org/t/pitch-inlinearray-type-sugar/79142))
9+
10+
## Introduction
11+
12+
We propose the introduction of type sugar for the `InlineArray` type, providing more succinct syntax for declaring an inline array.
13+
14+
## Motivation
15+
16+
[SE-0453](/proposals/0453-vector.md) introduced a new type, `InlineArray`, which includes a size parameter as part of its type:
17+
18+
```
19+
let fiveIntegers: InlineArray<5, Int> = .init(repeating: 99)
20+
```
21+
22+
Declaring this type is more cumbersome than its equivalent dyanmicaly-sized array, which has sugar for the type syntax:
23+
24+
```
25+
let fiveIntegers: [Int] = .init(repeating: 99, count: 5)
26+
```
27+
28+
This becomes more pronounced when dealing with multiple dimensions:
29+
30+
```
31+
let fiveByFive: InlineArray<5, InlineArray<5, Int>> = .init(repeating: .init(repeating: 99))
32+
```
33+
34+
## Proposed solution
35+
36+
A new sugared version of the `InlineArray` type is proposed:
37+
38+
```swift
39+
let fiveIntegers: [5 x Int] = .init(repeating: 99)
40+
```
41+
42+
The `x` here is the ASCII character, and is chosen to evoke the common shorthand use to represent "by", as in "4x4" or "2 in x 4 in".
43+
44+
Note that although it is used in the manner of an operator, `x` here serves more like a contextual keyword, similar to if the syntax were `[5 of Int]`.
45+
46+
## Detailed design
47+
48+
The new syntax consists of the value for the integer generic paramter and the type of the element generic paramter, separated by `x`.
49+
50+
This will be added to the grammar alongside the current type sugar:
51+
52+
> **Grammar of a type**
53+
> _type → sized-array-type_
54+
>
55+
> **Grammar of a sized array type**
56+
> _sized-array-type → [ expression `x` type ]_
57+
58+
Note that while the grammar allows for any expression, this is currently limited to only integer literals.
59+
60+
The new sugar is equivalent to declaring a type of `InlineArray`, so all rules that can be applied to the generic placeholders for the unsugared version also apply to the sugared version:
61+
62+
```
63+
// Nesting
64+
let fiveByFive: InlineArray<5, InlineArray<5, Int>> = .init(repeating: .init(repeating: 99))
65+
let fiveByFive: [5 x [5 x Int]] = .init(repeating: .init(repeating: 99))
66+
67+
// Inference from context:
68+
let fiveIntegers: [5 x _] = .init(repeating: 99)
69+
let fourBytes: [_ x Int8] = [1,2,3,4]
70+
let fourIntegers: [_ x _] = [1,2,3,4]
71+
72+
// use on rhs
73+
let fiveDoubles = [5 x _](repeating: 1.23)
74+
```
75+
76+
The sugar can also be used in place of the unsugared type wherever it might appear:
77+
78+
```
79+
[5 x Int](repeating: 99)
80+
MemoryLayout<[5 x Int]>.size
81+
unsafeBitCast((1,2,3), to: [3 x Int].self)
82+
```
83+
84+
There must be whitespace on either side of the separator i.e. you cannot write `[5x Int]`. There are no requirements to balance whitespace, `[5 x Int]` is permitted. A new line can appear after the `x` but not before it, as while this is not ambiguous, this aids with the parser recovery logic, leading to better syntax error diagnostics.
85+
86+
## Source Compatibility
87+
88+
Since it is not currently possible to write any form of the proposed syntax in Swift today, this proposal does not alter the meaning of any existing code.
89+
90+
## Impact on ABI
91+
92+
This is purely compile-time sugar for the existing type. It is resolved at compile time, and does not appear in the ABI nor rely on any version of the runtime.
93+
94+
## Future Directions
95+
96+
### Repeated value equivalent
97+
98+
Analogous to arrays, there is an equivalent _value_ sugar for literals of a specific size:
99+
100+
```
101+
// type inferred to be [5 x Int]
102+
let fiveInts = [5 x 99]
103+
// type inferred to be [5 x [5 x Int]]
104+
let fiveByFive = [5 x [5 x 99]]
105+
```
106+
107+
Unlike the sugar for the type, this would also have applicability for existing types:
108+
109+
```
110+
// equivalent to .init(repeating: 99, count: 5)
111+
let dynamic: [Int] = [5 x 99]
112+
```
113+
114+
This is a much bigger design space, potentially requiring a new expressible-by-literal protocol and a way to map the literal to an initializer. As such, it is left for a future proposal.
115+
116+
### Flattened multi-dimensional arrays
117+
118+
For multi-dimensional arrays, `[5 x [5 x Int]]` could be flattened to `[5 x 5 x Int]` without any additional parsing issues. This could be an alternative considered, but is in future directions as it could also be introduced as sugar for the former case at a later date.
119+
120+
## Alternatives Considered
121+
122+
### Choice of delimiter
123+
124+
The most obvious alternative here is the choice of separator. Other options include:
125+
126+
- `[5 * Int]`, using the standard ASCII symbol for multiplication.
127+
- `[5 ⨉ Int]`, the Unicode n-ary times operator. This looks nice but is impactical as not keyboard-accessible.
128+
- `[5; Int]` is what Rust uses, but appears to have little association with "times" or "many". Similarly other arbitrary punctuation e.g. `,` or `/` or `#`.
129+
- `[5 of Int]` is more verbose than `x` but could be considered more clear. It has the upside or downside, depending on your preference, of being almost, but not quite, grammatical.
130+
- `:` is of course ruled out as it is used for dictionary literals.
131+
132+
Note that `*` is an existing operator, and may lead to ambiguity in fuure when expressions can be used to determine the size: `[5 * N * Int]`. `x` is clearer in this case: `[5 * N x Int]`. It also avoids parsing ambiguity, as the grammar does not allow two identifiers in succession. But it would be less clear if `x` also appeared as an identifier: `[5 * x x Int]` (which is not yet permitted but may be in future use cases).
133+
134+
This becomes more important if the future direction of a value equivalent is pursued. `[2 * 2 * 2]` could be interpreted as `[2, 2, 2, 2]`, `[4, 4,]`, or `[8]`.
135+
136+
Since `x` cannot follow another identifier today, `[x x Int]` is unambiguous,[^type] but would clearly be hard to read. This is likely a hypothetical concern rather than a practical one. While `x` is used often in scratch code for a local variable, a more meaningful name is usually preferable, and this would be especially the case if it is found being used for the size of an array literal. In addition, while `i`, `j`, or `n` are often legitimate counters that might be suited to the size of an array, `x` is generally not used for such things.
137+
138+
[^type]: or even `[x x x]`, since `x` can be a type name, albeit one that defies Swift's naming conventions.
139+
140+
Another thing to consider is how that separator looks in the fully inferred version, which tend to start to look a little like ascii diagrams:
141+
142+
```
143+
[_ x _]
144+
[_ * _]
145+
[_; _]
146+
[_ of _]
147+
```
148+
149+
### Order of size and type
150+
151+
The order of size first, then type is determined by the ordering of the unsugared type, and deviating from this for the sugared version is not an option.
152+
153+
### Whitespace around the delimeter
154+
155+
In theory, when using integer literals or `_` the whitespace could be omitted (`[5x_]` is unabiguously `[5 x _]`). However, special casing allowing whitespace omission is not desirable.
156+
157+
### Choice of brackets
158+
159+
`InlineArray` has a lot in common with tuples – especially in sharing "copy on copy" behavior, unlike regular `Array`. So `(5 x Int)` may be an appropriate alternative to the square brackets, echoing this similarity.
160+
161+
Beyond varying the separator, there may be other dramatically different syntax that moves further from the "like Array sugar, but with a size argument".

0 commit comments

Comments
 (0)