Expand description
Synthesising Swift protocol conformances at run time.
Some SwiftUI modifiers are generic over a protocol whose conformances a
client is expected to declare — PreferenceKey above all. There is no
Swift source here to declare one, and the framework exports only its own
internal conformances, so the way to reach those modifiers is to build the
conformance the compiler would have emitted.
That is less daunting than it sounds, because of how Swift compiles
generics: a generic function receives the witness table as an ordinary
argument. Only a dynamic cast consults the global conformance table. So a
witness table built here and passed straight to View.preference(key:value:)
is used exactly as a compiler-emitted one would be — no registration
required.
§What a witness table is
One word per protocol requirement, in the order the protocol descriptor lists them, behind one word pointing at the conformance descriptor:
[0] conformance descriptor
[1] witness for requirement 0
[2] witness for requirement 1
...A requirement is a function pointer, except for an associated type, whose slot holds the type’s metadata directly. (The runtime treats a slot with bit 0 set as a mangled name to resolve instead; storing metadata avoids needing a generic environment at all.)
Requirements that carry a default implementation — the protocol descriptor
records one per requirement — need no witness from us: the default is
copied in. For PreferenceKey that leaves three to supply: the associated
Value, defaultValue, and reduce(value:nextValue:).
§Three things that are not optional
- The descriptor must point at the witness table through its
witness table pattern. A conformance lookup does not hand back a
table it was given; it materialises one from the pattern, and a null
pattern makes
swift_conformsToProtocolreport no conformance at all. - The conformance must be registered. Passing the table as a generic argument is not enough: building a generic type that constrains a parameter to the protocol makes the runtime look the conformance up.
- The conforming type must not be a standard library type. Registering
a conformance for
Swift.Intleaves the lookup still reporting none; a framework type registers and resolves normally.
None of the three fails loudly. Each surfaces as a null metadata response several calls later.
Structs§
- Conformance
- A synthesized conformance, kept alive for the process’s lifetime.
- Protocol
- A Swift protocol descriptor, read only as far as this needs.
Enums§
- Conformer
- What a conformance record says its conforming type is.
- Witness
- What to put in one slot of a witness table.
Functions§
- chart_
descriptor_ representable - An
AXChartDescriptorRepresentableconformance onSwiftUI.EdgeInsets. - identifiable
- An
Identifiableconformance onSwiftUI.EdgeInsets, withID == Int. - layout_
value_ key - A
LayoutValueKeyconformance whoseValueisSwift.Int. - observable
- An
Observableconformance onFoundation.JSONDecoder. - observable_
object - An
ObservableObjectconformance onFoundation.JSONDecoder. - preference_
key - A
PreferenceKeyconformance whoseValueisSwift.Int. - presentation_
sizing - A
PresentationSizingconformance forSwiftUI.EmptyView. - preview_
context - A
PreviewContextconformance forSwiftUI.EmptyView. - string_
identifiable - An
Identifiableconformance onSwift.String, withID == Int. - synthesize⚠
- Build a witness table for
conforming_type: protocol. - synthesize_
for ⚠ synthesizefor a conforming type that is not named by a nominal descriptor.- text_
renderer - A
TextRendererconformance onSwiftUI.EdgeInsets.