Skip to main content

Module conformance

Module conformance 

Source
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_conformsToProtocol report 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.Int leaves 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 AXChartDescriptorRepresentable conformance on SwiftUI.EdgeInsets.
identifiable
An Identifiable conformance on SwiftUI.EdgeInsets, with ID == Int.
layout_value_key
A LayoutValueKey conformance whose Value is Swift.Int.
observable
An Observable conformance on Foundation.JSONDecoder.
observable_object
An ObservableObject conformance on Foundation.JSONDecoder.
preference_key
A PreferenceKey conformance whose Value is Swift.Int.
presentation_sizing
A PresentationSizing conformance for SwiftUI.EmptyView.
preview_context
A PreviewContext conformance for SwiftUI.EmptyView.
string_identifiable
An Identifiable conformance on Swift.String, with ID == Int.
synthesize⚠
Build a witness table for conforming_type: protocol.
synthesize_for⚠
synthesize for a conforming type that is not named by a nominal descriptor.
text_renderer
A TextRenderer conformance on SwiftUI.EdgeInsets.