3
3
* Proposal: [ SE-0452] ( 0452-integer-generic-parameters.md )
4
4
* Authors: [ Alejandro Alonso] ( https://github.com/Azoy ) , [ Joe Groff] ( https://github.com/jckarter )
5
5
* Review Manager: [ Ben Cohen] ( https://github.com/airspeedswift )
6
- * Status: ** Active review (November 5-19 2024)**
6
+ * Status: ** Active review (Janyuary 6-14 2024)**
7
7
* 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 ) )
9
9
10
10
## Introduction
11
11
@@ -33,7 +33,7 @@ number of elements they consume or produce.
33
33
## Proposed solution
34
34
35
35
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:
37
37
38
38
``` swift
39
39
struct Vector <let count : Int , Element > {
@@ -54,11 +54,29 @@ Or it can be instantiated using integer generic parameters from the surrounding
54
54
generic environment:
55
55
56
56
``` 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 >>
59
59
}
60
60
```
61
61
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
+
62
80
Generic functions and methods can also be parameterized by integer generic
63
81
parameters. As with other generic parameters, the values of the generic
64
82
arguments for a call are inferred from the types of the argument values
@@ -156,6 +174,18 @@ func contrived() {
156
174
}
157
175
```
158
176
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
+
159
189
In a type reference, an integer generic argument can be provided as either
160
190
a literal integer, or as a reference to an integer generic parameter from
161
191
the enclosing generic context. References to type generic parameters,
@@ -556,6 +586,27 @@ generic parameters, like `Vector[count]<Element>`. This would avoid the need
556
586
for disambiguation if an arbitrary expression can be used as a `count` in
557
587
the future.
558
588
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
+
559
610
## Acknowledgments
560
611
561
612
We would like to thank the following people for prototyping and design
0 commit comments