Skip to content

Commit f6bff3b

Browse files
committed
Incorporate pitch thread feedback
1 parent 9938185 commit f6bff3b

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

proposals/NNNN-allow-additional-args-to-dynamicmemberlookup-subscripts.md

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ let _: String = C().member // (5); no ambiguity
211211
let _: String? = C().member // (6) preferred over (5); ⚠️ previously (5) ⚠️
212212
```
213213

214-
This last case is the only source of behavior change: (6) was previously not considered a valid candidate, but has a return type more specific than (5**, and is now picked at a callsite.
214+
This last case is the only source of behavior change: (6) was previously not considered a valid candidate, but has a return type more specific than (5), and is now picked at a callsite.
215215

216-
**In practice, it is expected that this situation is exceedingly rare.**
216+
In practice, it is expected that this situation is exceedingly rare.
217217

218218
## ABI compatibility
219219

@@ -225,45 +225,53 @@ The changes in this proposal require the adoption of a new version of the Swift
225225

226226
## Alternatives considered
227227

228-
The main alternative to this proposal is to not implement it. This is possible to work around using explicit methods such as `get()` and `set(_:)`:
229-
230-
```swift
231-
@dynamicMemberLookup
232-
struct Value {
233-
struct Property {
234-
func get(
235-
function: StaticString = #function,
236-
file: StaticString = #file,
237-
line: UInt = #line
238-
) -> Value {
239-
...
240-
}
241-
242-
func set(
243-
_ value: Value,
244-
function: StaticString = #function,
245-
file: StaticString = #file,
246-
line: UInt = #line
247-
) {
248-
...
249-
}
250-
}
251-
252-
subscript(dynamicMember member: String) -> Property { ... }
253-
}
254-
255-
let x: Value = ...
256-
let _ = x.member.get() // x.member
257-
x.member.set(Value(42)) // x.member = Value(42)
258-
```
259-
260-
However, this feels non-idiomatic, and for long chains of getters and setters, can become cumbersome:
261-
262-
```swift
263-
let x: Value = ...
264-
let _ = x.member.get().inner.get().nested.get() // x.member.inner.nested
265-
x.member.get().inner.get().nested.set(Value(42)) // x.member.inner.nested = Value(42)
266-
```
228+
The main alternative to this proposal is to not implement it, as:
229+
1. It was noted in [the pitch thread](https://forums.swift.org/t/pitch-allow-additional-arguments-to-dynamicmemberlookup-subscripts/79558) that allowing additional arguments to dynamic member lookup widens the gap in capabilities between dynamic members and regular members — dynamic members would be able to
230+
231+
1. Have caller side effects (i.e., have access to `#function`, `#file`, `#line`, etc.),
232+
2. Constrain themselves via generics, and
233+
3. Apply isolation to themselves via `#isolation`
234+
235+
where regular members cannot. However, (i) and (iii) are not considered an imbalance in functionality but instead are the raison d'être of this proposal. (ii) is also already possible today as dynamic member subscripts can be constrained via generics (and this is often used with keypath-based lookup).
236+
2. This is possible to work around using explicit methods such as `get()` and `set(_:)`:
237+
238+
```swift
239+
@dynamicMemberLookup
240+
struct Value {
241+
struct Property {
242+
func get(
243+
function: StaticString = #function,
244+
file: StaticString = #file,
245+
line: UInt = #line
246+
) -> Value {
247+
...
248+
}
249+
250+
func set(
251+
_ value: Value,
252+
function: StaticString = #function,
253+
file: StaticString = #file,
254+
line: UInt = #line
255+
) {
256+
...
257+
}
258+
}
259+
260+
subscript(dynamicMember member: String) -> Property { ... }
261+
}
262+
263+
let x: Value = ...
264+
let _ = x.member.get() // x.member
265+
x.member.set(Value(42)) // x.member = Value(42)
266+
```
267+
268+
However, this feels non-idiomatic, and for long chains of getters and setters, can become cumbersome:
269+
270+
```swift
271+
let x: Value = ...
272+
let _ = x.member.get().inner.get().nested.get() // x.member.inner.nested
273+
x.member.get().inner.get().nested.set(Value(42)) // x.member.inner.nested = Value(42)
274+
```
267275

268276
### Source compatibility
269277

0 commit comments

Comments
 (0)