Skip to content

Commit 7e16cb2

Browse files
slbenrimmington
andauthored
Apply suggestions from code review
Co-authored-by: Ben Rimmington <[email protected]>
1 parent fd77512 commit 7e16cb2

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

proposals/xxxx-extend-generic-static-member-lookup.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Authors: [Pavel Yaskevich](https://github.com/xedin), [Sam Lazarus](https://github.com/sl), [Matt Ricketson](https://github.com/ricketson)
55
* Review Manager: TBD
66
* Status: **Awaiting Review**
7-
* Implementation: https://github.com/apple/swift/pull/34523, [toolchain](https://ci.swift.org/job/swift-PR-toolchain-osx/770//artifact/branch-main/swift-PR-34523-770-osx.tar.gz) available.
7+
* Implementation: [apple/swift#34523](https://github.com/apple/swift/pull/34523)
88

99
## Introduction
1010

@@ -97,7 +97,7 @@ However, this approach has a few downsides:
9797
* **Repetitive:** Only the “Switch” component of the style name is important, since we already know that the modifier expects a type of `ToggleStyle`.
9898
* **Poor discoverability:** There is no autocomplete support to expose the available `ToggleStyle` types to choose from, so you have to know them in advance.
9999

100-
These downsides are impossible to avoid for generic parameters like above, which discourages generalizing functions. API designers should not have to chose between good design and easy-to-read code.
100+
These downsides are impossible to avoid for generic parameters like above, which discourages generalizing functions. API designers should not have to choose between good design and easy-to-read code.
101101

102102
Instead, we could ideally support leading dot syntax for generic types with known protocol conformances, allowing syntax like this:
103103

@@ -153,7 +153,7 @@ The type-checker is able to infer any protocol conformance requirements placed o
153153
The second option is a much better choice that avoids having to do a global lookup and conformance checking and is consistent with semantics of leading dot syntax, namely, the requirement that result and base types of the chain have to be equivalent. This leads to a new rule: if the result type of a static member conforms to the declaring protocol, it should be possible to reference such a member on a protocol metatype, using leading dot syntax, by implicitly replacing the protocol with a conforming type.
154154

155155

156-
> **Note:** If member returns a function type or an optional value type-checker considers “result type” (for purposes of base type inference) to be a result type of a function type and/or wrapped value of an optional type (if optional itself doesn’t conform to a required protocol). This enables calls to properties and optional chaining of member chains starting from protocol metatypes.
156+
> **Note:** If a member returns a function type or an optional value, the type-checker considers “result type” (for the purposes of base type inference) to be a result type of a function type and/or wrapped value of an optional type (if `Optional` itself doesn’t conform to a required protocol). This enables calls to properties, and optional chaining of member chains, starting from protocol metatypes.
157157
158158

159159
This approach works well for references without an explicit base, let’s consider an example:
@@ -177,11 +177,11 @@ applyStyle(.bordered)
177177
```
178178

179179

180-
In this case (`applyStyle(.bordered)`) the reference to the member `.bordered` is re-written to be `Bordered.bordered` in the type-checked AST.
180+
In this case (`applyStyle(.bordered)`) the reference to the member `.bordered` is re-written to be `BorderedStyle.bordered` in the type-checked AST.
181181

182182
To make this work the type-checker would attempt to infer protocol conformance requirements from context, e.g. the call site of a generic function (in this case there is only one such requirement - the protocol `Style`), and propagate them to the type variable representing the implicit base type of the chain. If there is no other contextual information available, e.g. the result type couldn’t be inferred to some concrete type, the type-checker would attempt to bind base to the type of the inferred protocol requirement.
183183

184-
Member lookup filtering is adjusted to find static members on protocol metatype base but the `Self` part of the reference type is replaced with result type of the discovered member (looking through function types and IUOs) and additional conformance requirements are placed on it (the new `Self` type) to make sure that the new base does conform to the expected protocol.
184+
Member lookup filtering is adjusted to find static members on a protocol metatype base, but the `Self` part of the reference type is replaced with the result type of the discovered member (looking through function types and IUOs) and additional conformance requirements are placed on it (the new `Self` type) to make sure that the new base does conform to the expected protocol.
185185

186186

187187
## Source compatibility

0 commit comments

Comments
 (0)