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
> Whereas placing a constraint on an extensionmember rather than the extension itself becomes possible:
30
+
> ```swift
31
+
> extension P {
32
+
>funcbar() whereSelf:Equatable { }
33
+
> }
34
+
> ```
29
35
30
36
Swift-evolution thread: [Discussion thread topic for that proposal](https://forums.swift.org/t/where-clauses-on-contextually-generic-declaractions/22449)
31
37
32
38
## Motivation
33
39
34
-
Today, `where` clauses on contextually generic declarations are expressed indirectly by placing them inside conditional extensions. Unless constraints are identical, every such declaration requires a separate extension.
40
+
Today, `where` clauses on contextually generic declarations, including protocolextension members, are expressed indirectly by placing them inside conditional extensions. Unless constraints are identical, every such declaration requires a separate extension.
35
41
This dependence on extensions can be an obstacle to grouping semantically related APIs, stacking up constraints and,
36
42
sometimes, the legibility of heavily generic interfaces.
37
43
38
44
It is reasonable to expect a `where` clause to work anywhere a constraint can be meaningfully imposed, meaning both of these structuring styles should be available to the user:
39
45
40
46
```swift
41
-
struct Foo<T>// 'Foo' can be any kind of nominal type declaration. For a protocol, 'T' would be an associatedtype.
47
+
// 'Foo' can be any kind of nominal type declaration.
48
+
// For a protocol, 'T' would be an associatedtype.
49
+
struct Foo<T>
42
50
43
51
extension Foo where T:Sequence, T.Element:Equatable {
44
52
funcslowFoo() { ... }
@@ -68,5 +76,4 @@ This is an additive change with no impact on the ABI and existing code.
68
76
69
77
## Effect on API resilience
70
78
71
-
For public declarations in resilient libraries, switching between a constrained extension and a «direct» `where` clause
72
-
will not be a source-breaking change, but it most likely will break the ABI due to subtle mangling differences.
79
+
For public declarations in resilient libraries, moving a constraint from an extension to a member and vice versa will not be a source-breaking change, but will break the ABI due to subtle mangling differences.
0 commit comments