1
- # Replace ` typealias ` keyword with ` associated ` for associated type declarations
1
+ # Replace ` typealias ` keyword with ` associatedtype ` for associated type declarations
2
2
3
3
* Proposal: [ SE-0011] ( https://github.com/apple/swift-evolution/blob/master/proposals/0011-replace-typealias-associated.md )
4
4
* Author(s): [ Loïc Lecrenier] ( https://github.com/loiclec )
@@ -16,7 +16,7 @@ These two kinds of declarations are different and should use distinct keywords.
16
16
This would emphasize the difference between them and reduce some of the
17
17
confusion surrounding the use of associated types.
18
18
19
- The proposed new keyword is ` associated ` .
19
+ The proposed new keyword is ` associatedtype ` .
20
20
21
21
## Motivation
22
22
@@ -26,7 +26,7 @@ Re-using `typealias` for associated type declarations is confusing in many ways.
26
26
other places.
27
27
2 . It hides the existence of associated types to beginners, which allows them
28
28
to write code they misunderstand.
29
- 3 . It hides the absence of concrete type aliases inside protocols.
29
+ 3 . It is not clear that concrete type aliases are forbidden inside protocols.
30
30
31
31
In particular, ** 2 + 3** leads to programmers writing
32
32
@@ -59,7 +59,7 @@ understand.
59
59
60
60
## Proposed solution
61
61
62
- For declaring associated types, replace the ` typealias ` keyword with ` associated ` .
62
+ For declaring associated types, replace the ` typealias ` keyword with ` associatedtype ` .
63
63
64
64
This solves the issues mentioned above:
65
65
@@ -72,30 +72,30 @@ This eliminates the confusion showed in the previous code snippets.
72
72
73
73
``` swift
74
74
protocol Prot {
75
- associated Container : SequenceType
75
+ associatedtype Container : SequenceType
76
76
typealias Element = Container.Generator .Element // error: cannot declare type alias inside protocol, use protocol extension instead
77
77
}
78
78
```
79
79
80
80
``` swift
81
81
protocol Prot {
82
- associated Container : SequenceType
82
+ associatedtype Container : SequenceType
83
83
}
84
84
extension Prot {
85
85
typealias Element = Container.Generator .Element
86
86
}
87
87
```
88
88
89
- Alternative keywords considered: ` withtype ` , ` associatedtype ` , ` typeassociation ` , ` type `
89
+ Alternative keywords considered: ` type ` , ` associated ` , ` requiredtype ` , ` placeholdertype ` , …
90
90
91
91
## Proposed Approach
92
92
93
- For declaring associated types, I suggest adding ` associated ` and deprecating
93
+ For declaring associated types, I suggest adding ` associatedtype ` and deprecating
94
94
` typealias ` in Swift 2.2, and removing ` typealias ` entirely in Swift 3.
95
95
96
96
## Impact on existing code
97
97
98
- As it simply replaces one keyword for another, the transition to ` associated `
98
+ As it simply replaces one keyword for another, the transition to ` associatedtype `
99
99
could be easily automated without any risk of breaking existing code.
100
100
101
101
## Mailing List
0 commit comments