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
publicstaticvar `default`: StaticMember<DefaultToggleStyle> { get }
134
+
publicstaticvar `switch`: StaticMember<SwitchToggleStyle> { get }
135
+
publicstaticvar checkbox: StaticMember<CheckboxToggleStyle> { get }
133
136
}
134
137
135
-
MyView().toggleStyle(.switch)
136
-
MyView().toggleStyle(.checkbox)
138
+
// Leading dot syntax (using rejected workaround):
139
+
140
+
Toggle("Wi-Fi", isOn: $isWiFiEnabled)
141
+
.toggleStyle(.switch)
137
142
```
138
143
139
144
However, `StaticMember`*serves no purpose* outside of achieving a more ideal syntax elsewhere. Its inclusion is hard to comprehend for anyone looking at the public facing API, as the type itself is decoupled from its actual purpose. SwiftUI removed `StaticMember` before exiting beta for exactly that reason: developers were commonly confused by its existence, declaration complexity, and usage within the framework.
@@ -164,24 +169,33 @@ The second option is a much better choice that avoids having to do a global look
164
169
This approach works well for references without an explicit base, let’s consider an example:
publicstaticvar `default`: DefaultToggleStyle { get }
188
+
publicstaticvar `switch`: SwitchToggleStyle { get }
189
+
publicstaticvar checkbox: CheckboxToggleStyle { get }
175
190
}
176
191
177
-
funcapplyStyle<S: ToggleStyle>(_: S) {
178
-
// ...
179
-
}
192
+
// Leading dot syntax (using proposed solution):
180
193
181
-
applyStyle(.switch)
194
+
Toggle("Wi-Fi", isOn: $isWiFiEnabled)
195
+
.toggleStyle(.switch)
182
196
```
183
197
184
-
In this case (`applyStyle(.switch)`) the reference to the member `.switch` is re-written to be `SwitchToggleStyle.switch` in the type-checked AST.
198
+
In the case of `.toggleStyle(.switch)`, the reference to the member `.switch` is re-written to be `SwitchToggleStyle.switch` in the type-checked AST.
185
199
186
200
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 `ToggleStyle`), 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.
0 commit comments