Skip to content

Commit b1923a4

Browse files
authored
Homogenize KeyPath and update diagnostic.
1 parent 3f0d0cd commit b1923a4

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

proposals/0438-metatype-keypath.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Metatype Keypaths
1+
# Metatype KeyPaths
22

33
* Proposal: [SE-0438](0438-metatype-keypath.md)
44
* Authors: [Amritpan Kaur](https://github.com/amritpan), [Pavel Yaskevich](https://github.com/xedin)
@@ -9,15 +9,15 @@
99

1010
## Introduction
1111

12-
Key path expressions access properties dynamically. They are declared with a concrete root type and one or more key path components that define a path to a resulting value via the type’s properties, subscripts, optional-chaining expressions, forced unwrapped expressions, or self. This proposal expands key path expression access to include static properties of a type, i.e., metatype keypaths.
12+
KeyPath expressions access properties dynamically. They are declared with a concrete root type and one or more KeyPath components that define a path to a resulting value via the type’s properties, subscripts, optional-chaining expressions, forced unwrapped expressions, or self. This proposal expands KeyPath expression access to include static properties of a type, i.e., metatype KeyPaths.
1313

1414
## Motivation
1515

16-
Metatype keypaths were briefly explored in the pitch for [SE-0254](https://forums.swift.org/t/pitch-static-and-class-subscripts/21850) and the [proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0254-static-subscripts.md#metatype-key-paths) later recommended them as a future direction. Allowing key path expressions to directly refer to static properties has also been discussed on the Swift Forums for database lookups when used [in conjunction with @dynamicMemberLookup](https://forums.swift.org/t/dynamic-key-path-member-lookup-cannot-refer-to-static-member/30212) and as a way to avoid verbose hacks like [referring to a static property through another computed property](https://forums.swift.org/t/key-path-cannot-refer-to-static-member/28055). Supporting metatype keypaths in the Swift language will address these challenges and improve language semantics.
16+
Metatype KeyPaths were briefly explored in the pitch for [SE-0254](https://forums.swift.org/t/pitch-static-and-class-subscripts/21850) and the [proposal](https://github.com/apple/swift-evolution/blob/main/proposals/0254-static-subscripts.md#metatype-key-paths) later recommended them as a future direction. Allowing KeyPath expressions to directly refer to static properties has also been discussed on the Swift Forums for database lookups when used [in conjunction with @dynamicMemberLookup](https://forums.swift.org/t/dynamic-key-path-member-lookup-cannot-refer-to-static-member/30212) and as a way to avoid verbose hacks like [referring to a static property through another computed property](https://forums.swift.org/t/key-path-cannot-refer-to-static-member/28055). Supporting metatype KeyPaths in the Swift language will address these challenges and improve language semantics.
1717

1818
## Proposed solution
1919

20-
We propose to allow keypath expressions to define a reference to static properties. The following usage, which currently generates a compiler error, will be allowed as valid Swift code.
20+
We propose to allow KeyPath expressions to define a reference to static properties. The following usage, which currently generates a compiler error, will be allowed as valid Swift code.
2121

2222
```swift
2323
struct Bee {
@@ -31,24 +31,24 @@ let kp = \Bee.Type.name
3131

3232
### Metatype syntax
3333

34-
Keypath expressions where the first component refers to a static property will include `.Type` on their root types stated in the key path contextual type or in the key path literal. For example:
34+
KeyPath expressions where the first component refers to a static property will include `.Type` on their root types stated in the KeyPath contextual type or in the KeyPath literal. For example:
3535

3636
```swift
3737
struct Bee {
3838
static let name = "honeybee"
3939
}
4040

41-
let kpWithContextualType: KeyPath<Bee.Type, String> = \.name // key path contextual root type of Bee.Type
42-
let kpWithLiteral = \Bee.Type.name // key path literal \Bee.Type
41+
let kpWithContextualType: KeyPath<Bee.Type, String> = \.name // KeyPath contextual root type of Bee.Type
42+
let kpWithLiteral = \Bee.Type.name // KeyPath literal \Bee.Type
4343
```
4444

45-
Attempting to write the above metatype keypath without including `.Type will trigger an error diagnostic:
45+
Attempting to write the above metatype KeyPath without including `.Type` will trigger an error diagnostic with a fix-it recommending the addition of `Type` after the root type `Bee`:
4646

4747
```swift
4848
let kpWithLiteral = \Bee.name // error: static member 'name' cannot be used on instance of type 'Bee'
4949
```
5050

51-
Keypath expressions where the component referencing a static property is not the first component do not require `.Type`:
51+
KeyPath expressions where the component referencing a static property is not the first component do not require `.Type`:
5252
```swift
5353
struct Species {
5454
static let isNative = true
@@ -62,7 +62,7 @@ let kpSecondComponentIsStatic = \Wasp.species.isNative
6262
```
6363
### Access semantics
6464

65-
Immutable static properties will form the read-only keypaths just like immutable instance properties.
65+
Immutable static properties will form the read-only KeyPaths just like immutable instance properties.
6666
```swift
6767
struct Tip {
6868
static let isIncluded = True
@@ -72,7 +72,7 @@ struct Tip {
7272
let kpStaticImmutable: KeyPath<Tip.Type, Bool> = \.isIncluded
7373
let kpInstanceImmutable: KeyPath<Tip, Bool> = \.isVoluntary
7474
```
75-
However, unlike instance members, keypaths to mutable static properties will always conform to `ReferenceWritableKeyPath` because metatypes are reference types.
75+
However, unlike instance members, KeyPaths to mutable static properties will always conform to `ReferenceWritableKeyPath` because metatypes are reference types.
7676
```swift
7777
struct Tip {
7878
static var total = 0
@@ -84,7 +84,7 @@ let kpInstanceMutable: WriteableKeyPath<Tip, Int> = \.flatRate
8484
```
8585
## Effect on source compatibility
8686

87-
This feature breaks source compatibility for key path expressions that reference static properties after subscript overloads. For example, the compiler cannot differentiate between subscript keypath components by return type in the following:
87+
This feature breaks source compatibility for KeyPath expressions that reference static properties after subscript overloads. For example, the compiler cannot differentiate between subscript KeyPath components by return type in the following:
8888

8989
```swift
9090
struct S {
@@ -99,7 +99,7 @@ struct Test {
9999
let kpViaSubscript = \Test.[42] // fails to typecheck
100100
```
101101

102-
This keypath does not specify a contextual type, without which the key path value type is unknown. To form a keypath to the metatype subscript and return an `Int`, we can specify a contextual type with a value type of `S.Type` and chain the metatype keypath:
102+
This KeyPath does not specify a contextual type, without which the KeyPath value type is unknown. To form a KeyPath to the metatype subscript and return an `Int`, we can specify a contextual type with a value type of `S.Type` and chain the metatype KeyPath:
103103

104104
```swift
105105
let kpViaSubscript: KeyPath<Test, S.Type> = \Test.[42]
@@ -114,9 +114,9 @@ This feature does not affect ABI compatibility and has no implications on adopti
114114

115115
### Key Paths to Enum cases
116116

117-
Adding language support for read-only key paths to enum cases has been widely discussed on the [Swift Forums](https://forums.swift.org/t/enum-case-key-paths-an-update/68436) but has been left out of this proposal as this merits a separate discussion around [syntax design and implementation concerns](https://forums.swift.org/t/enum-case-keypaths/60899/32).
117+
Adding language support for read-only KeyPaths to enum cases has been widely discussed on the [Swift Forums](https://forums.swift.org/t/enum-case-key-paths-an-update/68436) but has been left out of this proposal as this merits a separate discussion around [syntax design and implementation concerns](https://forums.swift.org/t/enum-case-keypaths/60899/32).
118118

119-
Since references to enum cases must be metatypes, extending keypath expressions to include references to metatypes will hopefully bring the Swift language closer to adopting keypaths to enum cases in a future pitch.
119+
Since references to enum cases must be metatypes, extending KeyPath expressions to include references to metatypes will hopefully bring the Swift language closer to adopting KeyPaths to enum cases in a future pitch.
120120

121121
## Acknowledgments
122122

0 commit comments

Comments
 (0)