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
Copy file name to clipboardExpand all lines: proposals/0438-metatype-keypath.md
+8-8Lines changed: 8 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ Metatype keypaths were briefly explored in the pitch for [SE-0254](https://forum
19
19
20
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.
21
21
22
-
```
22
+
```swift
23
23
structBee {
24
24
staticlet name ="honeybee"
25
25
}
@@ -33,7 +33,7 @@ let kp = \Bee.Type.name
33
33
34
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:
Attempting to write the above metatype keypath without including `.Type will trigger an error diagnostic:
46
46
47
-
```
47
+
```swift
48
48
let kpWithLiteral = \Bee.name// error: static member 'name' cannot be used on instance of type 'Bee'
49
49
```
50
50
51
51
Keypath expressions where the component referencing a static property is not the first component do not require `.Type`:
52
-
```
52
+
```swift
53
53
structSpecies {
54
54
staticlet isNative =true
55
55
}
@@ -63,7 +63,7 @@ let kpSecondComponentIsStatic = \Wasp.species.isNative
63
63
### Access semantics
64
64
65
65
Immutable static properties will form the read-only keypaths just like immutable instance properties.
66
-
```
66
+
```swift
67
67
structTip {
68
68
staticlet isIncluded = True
69
69
let isVoluntary = False
@@ -73,7 +73,7 @@ let kpStaticImmutable: KeyPath<Tip.Type, Bool> = \.isIncluded
73
73
let kpInstanceImmutable: KeyPath<Tip, Bool> = \.isVoluntary
74
74
```
75
75
However, unlike instance members, keypaths to mutable static properties will always conform to `ReferenceWritableKeyPath` because metatypes are reference types.
76
-
```
76
+
```swift
77
77
structTip {
78
78
staticvar total =0
79
79
var flatRate =20
@@ -86,7 +86,7 @@ let kpInstanceMutable: WriteableKeyPath<Tip, Int> = \.flatRate
86
86
87
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:
88
88
89
-
```
89
+
```swift
90
90
structS {
91
91
staticvar count: Int { 42 }
92
92
}
@@ -101,7 +101,7 @@ let kpViaSubscript = \Test.[42] // fails to typecheck
101
101
102
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:
103
103
104
-
```
104
+
```swift
105
105
let kpViaSubscript: KeyPath<Test, S.Type> = \Test.[42]
106
106
let kpAppended = kpViaSubscript.appending(path: \.count)
0 commit comments