Skip to content

Commit 5450c01

Browse files
jckarterbenrimmingtonairspeedswift
authored
Update SE-0452 in response to LSG review (#2631)
* Update SE-0452 in response to LSG review * Update proposals/0452-integer-generic-parameters.md Co-authored-by: Ben Rimmington <[email protected]> * Update 0452-integer-generic-parameters.md --------- Co-authored-by: Ben Rimmington <[email protected]> Co-authored-by: Ben Cohen <[email protected]>
1 parent 80c3616 commit 5450c01

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

proposals/0452-integer-generic-parameters.md

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Proposal: [SE-0452](0452-integer-generic-parameters.md)
44
* Authors: [Alejandro Alonso](https://github.com/Azoy), [Joe Groff](https://github.com/jckarter)
55
* Review Manager: [Ben Cohen](https://github.com/airspeedswift)
6-
* Status: **Active review (November 5-19 2024)**
6+
* Status: **Active review (Janyuary 6-14 2024)**
77
* Implementation: [apple/swift#75518](https://github.com/apple/swift/pull/75518)
8-
* Review: ([pitch](https://forums.swift.org/t/integer-generic-parameters/74181)) ([review](https://forums.swift.org/t/se-0452-integer-generic-parameters/75844))
8+
* Review: ([pitch](https://forums.swift.org/t/integer-generic-parameters/74181)) ([first review](https://forums.swift.org/t/se-0452-integer-generic-parameters/75844)) ([second review](https://forums.swift.org/t/second-review-se-0452-integer-generic-parameters/77043))
99

1010
## Introduction
1111

@@ -33,7 +33,7 @@ number of elements they consume or produce.
3333
## Proposed solution
3434

3535
Generic types can now be parameterized by integer parameters, declared using
36-
the syntax `let <Name>: Int` inside of the generic parameter angle brackets:
36+
the syntax `let <name>: Int` inside of the generic parameter angle brackets:
3737

3838
```swift
3939
struct Vector<let count: Int, Element> {
@@ -54,11 +54,29 @@ Or it can be instantiated using integer generic parameters from the surrounding
5454
generic environment:
5555

5656
```swift
57-
struct Matrix<let n: Int, let m: Int> {
58-
var matrix: Vector<n, Vector<m, Double>>
57+
struct Matrix<let columns: Int, let rows: Int> {
58+
var matrix: Vector<columns, Vector<rows, Double>>
5959
}
6060
```
6161

62+
Integer generic parameters become static constant members of the type, with
63+
the same visibility as the type itself:
64+
65+
```swift
66+
public struct Matrix<let columns: Int, let rows: Int> {
67+
// implicitly has these members:
68+
// public static var columns: Int { get }
69+
// public static var rows: Int { get }
70+
}
71+
72+
// From another module:
73+
74+
import struct Matrices.Matrix
75+
76+
print(Matrix<4, 3>.columns) // prints 4
77+
print(Matrix<4, 3>.rows) // prints 3
78+
```
79+
6280
Generic functions and methods can also be parameterized by integer generic
6381
parameters. As with other generic parameters, the values of the generic
6482
arguments for a call are inferred from the types of the argument values
@@ -156,6 +174,18 @@ func contrived() {
156174
}
157175
```
158176

177+
Integer generic parameters of types become static members of that type,
178+
with the same visibility as the type itself. It is an error to try to
179+
declare another static property with the same name as an integer generic
180+
parameter within the type declaration, just as it would if the property
181+
were independently declared:
182+
183+
```swift
184+
struct Vec<let count: Int> {
185+
static let count: Int // error: Vec already has a static property `count`
186+
}
187+
```
188+
159189
In a type reference, an integer generic argument can be provided as either
160190
a literal integer, or as a reference to an integer generic parameter from
161191
the enclosing generic context. References to type generic parameters,
@@ -556,6 +586,27 @@ generic parameters, like `Vector[count]<Element>`. This would avoid the need
556586
for disambiguation if an arbitrary expression can be used as a `count` in
557587
the future.
558588

589+
### Behavior of static properties corresponding to generic parameters
590+
591+
In response to the first round of review, we added static properties
592+
corresponding to the integer generic parameters of types, based on feedback
593+
that, in many if not most cases, the values of the generic parameters would end
594+
up being redeclared as static parameters, and the existence and names of the
595+
generic parameters are already essentially part of its public API, since
596+
usage of the type must be able to provide arguments to the parameters, and
597+
extensions can refer to the parameters by their names.
598+
599+
One good argument against doing this is that we don't already do anything
600+
analogous for type generic parameters, such as presenting them as a typealias
601+
member of the type. We think that it would be beneficial to do so, however,
602+
and in discussion with the LSG, there were attempts to make this happen in
603+
the past, but they ran into source compatibility issues. If there is a chance
604+
to take a source break that enables this functionality for type generic
605+
parameters, the LSG thinks it is worth considering. With integer generic
606+
parameters, we have the opportunity to do the right thing out of the gate,
607+
and we would have similar source-breaking considerations to do it later, so
608+
we believe it is better to do the right thing up front.
609+
559610
## Acknowledgments
560611

561612
We would like to thank the following people for prototyping and design

0 commit comments

Comments
 (0)