You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -93,151 +93,217 @@ e.g., a tuple type `(T0, Int, (T0) -> Int)` involving the type
93
93
variable `T0`.
94
94
95
95
There are a number of different kinds of constraints used to describe
96
-
the Swift type system:
97
-
98
-
**Equality**
99
-
An equality constraint requires two types to be identical. For
100
-
example, the constraint `T0 == T1` effectively ensures that `T0` and
101
-
`T1` get the same concrete type binding. There are two different
102
-
flavors of equality constraints:
103
-
104
-
- Exact equality constraints, or "binding", written `T0 := X`
105
-
for some type variable `T0` and type `X`, which requires
106
-
that `T0` be exactly identical to `X`;
107
-
- Equality constraints, written `X == Y` for types `X` and
108
-
`Y`, which require `X` and `Y` to have the same type,
109
-
ignoring lvalue types in the process. For example, the
110
-
constraint `T0 == X` would be satisfied by assigning `T0`
111
-
the type `X` and by assigning `T0` the type `@lvalue X`.
112
-
113
-
**Subtyping**
114
-
A subtype constraint requires the first type to be equivalent to or
115
-
a subtype of the second. Subtyping constraints are written `X < Y`.
116
-
117
-
The following are the basic subtyping rules in the Swift language.
118
-
First, we have class inheritance relationships:
119
-
- If `X` is a subclass of `Y`, then `X < Y`.
120
-
- If `X` is an archetype, and `Y` is the superclass bound of `X`, then `X < Y`.
121
-
122
-
Second, we have existential erasure:
123
-
- If `X` is a concrete type, and `X` conforms to a protocol `P`, then `X < any P`.
124
-
- If `X` is an archetype, and `X` conforms to a protocol `P`, then `X < any P`.
125
-
- If `X` is a class or class-bound archetype, then `X < AnyObject`.
126
-
- If `X` is a type, `X < any P`, and `X < any Q`, then `X < any P & Q`.
127
-
128
-
Third, we have conversions between existential types:
129
-
- If protocol `P` inherits from protocol `Q`, then `any P < any Q`.
130
-
- If `any P` and `any Q` are existential types, then `any P & Q < any P` and `any P & Q < any Q`.
131
-
- If `P` is a protocol and `C` is a class, then `any P < any P & C`.
132
-
- If `P` is a protocol, then `any P<...> < any P`.
133
-
134
-
An existential type converts to a class in a few special cases:
135
-
- If `P` is a protocol and `C` is a class, then `any P & C < any P`.
136
-
- If `P` is a protocol and `C` is a class, then `any P & C < C`.
137
-
- If `P` is a protocol and `C` is the superclass bound of `P`, then `any P < C`.
138
-
139
-
Functions:
140
-
- If `X < Y`, then `(..., Y, ...) -> Z < (..., X, ...) -> Z` (function parameters are contravariant).
141
-
- If `X < Y`, then `(...) -> X < (...) -> Y` (function results are covariant).
142
-
143
-
Tuples:
144
-
- If `X < Y`, then `(..., X, ...) < (..., Y, ...)` (tuple elements are covariant).
145
-
146
-
Optionals:
147
-
- If `X` is an arbitrary type, then `X < Optional<X>`.
148
-
- If `X < Y`, then `Optional<X> < Optional<Y>`.
149
-
150
-
Metatypes:
151
-
- If `X < Y` via class inheritance, existential erasure, and existential conversion, then `X.Type < Y.Type`. However, this does not hold for arbitrary subtype relationships; for example, `X.Type < Optional<X>.Type` does not hold.
152
-
153
-
Collections:
154
-
- If `X < Y`, then `Array<X> < Array<Y>`.
155
-
- If `K1 < K2` and `V1 < V2`, then `Dictionary<K1, K2> < Dictionary<V1, V2>`.
156
-
- If `X < Y`, then `Set<X> < Set<Y>`.
157
-
158
-
Toll-free bridging:
159
-
-`NSString < CFString` and also `CFString < NSString`.
160
-
- Similar conversions exist for various other CF/NS types that
161
-
we won't discuss here.
162
-
163
-
CGFloat:
164
-
-`CGFloat < Double` and `Double < CGFloat`, but with certain restrictions.
165
-
166
-
AnyHashable:
167
-
- If `X` is an archetype or concrete type that conforms to
168
-
`Hashable`, then `X < AnyHashable`.
169
-
170
-
Metatype to AnyObject:
171
-
- If Objective-C interop is enabled and `X` is an arbitrary type,
172
-
then `X.Type < AnyObject`.
96
+
the Swift type system. This only lists the most important kinds; for
97
+
the full list, see `include/swift/Sema/Constraint.h`.
98
+
99
+
### Bind
100
+
101
+
Written `X := Y` for arbitrary types `X` and `Y`, this constraint
102
+
requires that `X` and `Y` can both be derived by performing substitution
103
+
on some common type `Z`.
104
+
105
+
### Equal
106
+
107
+
Equality constraints, written `X == Y` for types `X` and `Y`, are
108
+
satisfied if and only if erasing `@lvalue` types from `X` and `Y`
109
+
yields a pair of types that would satisfy a **Bind** constraint.
110
+
111
+
### BindParam
112
+
113
+
BindParam constraints appear when type checking closures. Given two
114
+
types `X` and `Y`, `X BindParam Y` is satisfied if `X := Y`, or if
115
+
`X` is an `@lvalue` type, `Y` is an `inout` type, and stripping off
116
+
the specifiers from both sides yields a pair of types that would
117
+
satisfy a **Bind** constraint.
118
+
119
+
### Subtype
120
+
121
+
A Subtype constraint requires the first type to be equivalent to, or
122
+
a subtype of, the second. Subtyping constraints are written `X < Y`.
123
+
124
+
However, be warned that this relation is neither anti-reflexive
125
+
(because `X < X` for all `X`), nor is it transitive (because `X < Y`
126
+
and `Y < Z` does not always imply that `X < Z`. For example, suppose
127
+
that `X` does not conform to `P`, but `Optional<X>` does. Then we have
128
+
`X < Optional<X>` and `Optional<X> < any P`, but _not_`X < any P`).
129
+
130
+
The following are the basic subtyping rules in the Swift language.
131
+
132
+
First, we have class inheritance relationships:
133
+
- If `X` is a subclass of `Y`, then `X < Y`.
134
+
- If `X` is an archetype, and `Y` is the superclass bound of `X`, then `X < Y`.
135
+
136
+
Second, we have existential erasure:
137
+
- If `X` is a concrete type, and `X` conforms to a protocol `P`, then `X < any P`.
138
+
- If `X` is an archetype, and `X` conforms to a protocol `P`, then `X < any P`.
139
+
- If `X` is a class or class-bound archetype, then `X < AnyObject`.
140
+
- If `X` is a type, `X < any P`, and `X < any Q`, then `X < any P & Q`.
141
+
142
+
Third, we have conversions between existential types:
143
+
- If protocol `P` inherits from protocol `Q`, then `any P < any Q`.
144
+
- If `any P` and `any Q` are existential types, then `any P & Q < any P` and `any P & Q < any Q`.
145
+
- If `P` is a protocol and `C` is a class, then `any P < any P & C`.
146
+
- If `P` is a protocol, then `any P<...> < any P`.
147
+
148
+
An existential type converts to a class in a few special cases:
149
+
- If `P` is a protocol and `C` is a class, then `any P & C < any P`.
150
+
- If `P` is a protocol and `C` is a class, then `any P & C < C`.
151
+
- If `P` is a protocol and `C` is the superclass bound of `P`, then `any P < C`.
152
+
153
+
Functions:
154
+
- If `X < Y`, then `(..., Y, ...) -> Z < (..., X, ...) -> Z` (function parameters are contravariant).
155
+
- If `X < Y`, then `(...) -> X < (...) -> Y` (function results are covariant).
156
+
157
+
Tuples:
158
+
- If `X < Y`, then `(..., X, ...) < (..., Y, ...)` (tuple elements are covariant).
159
+
- Tuple labels can be eliminated, so `(X, Y) < (x: X, y: Y)`.
160
+
- Tuple labels can be introduced, so `(x: X, y: Y) < (X, Y)`.
161
+
162
+
Optionals:
163
+
- If `X` is an arbitrary type, then `X < Optional<X>`.
164
+
- If `X < Y`, then `Optional<X> < Optional<Y>`.
165
+
166
+
Metatypes:
167
+
- If `X < Y` via class inheritance, existential erasure, and existential conversion,
168
+
then `X.Type < Y.Type`. However, this does not extend to other subtype
169
+
relationships; for example, `X.Type < Optional<X>.Type` does not hold.
170
+
171
+
Collections:
172
+
- If `X < Y`, then `Array<X> < Array<Y>`.
173
+
- If `K1 < K2` and `V1 < V2`, then `Dictionary<K1, K2> < Dictionary<V1, V2>`.
174
+
- If `X < Y`, then `Set<X> < Set<Y>`.
175
+
176
+
Toll-free bridging:
177
+
-`NSString < CFString` and also `CFString < NSString`.
178
+
- Similar conversions exist for various other CF/NS types that
179
+
we won't discuss here.
180
+
181
+
CGFloat:
182
+
-`CGFloat < Double` and `Double < CGFloat`, but with certain restrictions.
183
+
184
+
AnyHashable:
185
+
- If `X` is an archetype or concrete type that conforms to
186
+
`Hashable`, then `X < AnyHashable`.
187
+
188
+
Metatype to AnyObject:
189
+
- If Objective-C interop is enabled and `X` is an arbitrary type,
190
+
then `X.Type < AnyObject`.
173
191
174
192
**Conversion**
175
193
176
-
The conversion relation is a superset of the subtype relation. Conversion
177
-
constraints are written `X <c Y`, read as "`X` can be converted to `Y`".
178
-
179
-
Today, the main difference is that conversion allows _tuple shuffles_, where
180
-
the elements of a tuple are re-ordered by considering labels. For example,
181
-
`(x: Int, y: String) <c (y: String, x: Int)` is true, but
0 commit comments