Skip to content

MetadataAccessFunction

Azoy edited this page Mar 17, 2021 · 2 revisions

MetadataAccessFunction

The Metadata Access Function is a special function within every type context descriptor that instantiates metadata of that type.

public struct MetadataAccessFunction

To give an example, consider the following:

struct Foo {}
struct Bar<T> {}

In this example, Foo's metadata access function will just return the already cached metadata, Foo.self. Bar in this case is special because of the generic parameter meaning that any type can be held in there. The Swift runtime does not instantiate every single type metadata combination for Bar, instead it instantiates type metadata for combinations that are asked for through this metadata access function. For example:

// In this case we ask for the Bar<Int> which the Swift runtime will
// generate metadata for, but in our case we just want the context
// descriptor.
let metadata = reflectStruct(Bar<Int>.self)!
let accessor = metadata.descriptor.accessor

// Here we call the accessor to give us complete metadata with the first
// argument to be a `String`.
print(accessor(.complete, String.self).type) // Bar<String>

Methods

callAsFunction(_:)

Calls the metadata access function.

public func callAsFunction(_ request: MetadataRequest) -> Echo.MetadataResponse

Parameters

  • request: MetadataRequest that instructs what state of metadata we want and whether we want to block or not. In most cases just ask for .complete.

Returns

MetadataResponse that has our requested metadata and tells us what state that metadata is in.

callAsFunction(_:_:)

Calls the metadata access function for a type who has no conformances on their generic parameters.

public func callAsFunction(_ request: MetadataRequest, _ args: Any.Type) -> Echo.MetadataResponse

Parameters

  • request: MetadataRequest that instructs what state of metadata we want and whether we want to block or not. In most cases just ask for .complete.
  • args: A variadic parameter for including generic arguments to types that have generic requirements in their type signature. This field MUST be correct for the function to work correctly.

Returns

MetadataResponse that has our requested metadata and tells us what state that metadata is in.

callAsFunction(_:_:)

Calls the metadata access function for a type who has generic arguments that require conformances.

public func callAsFunction(_ request: MetadataRequest, _ args: (Any.Type, WitnessTable?)) -> Echo.MetadataResponse

Example:

protocol Testable {}
extension Int: Testable {}
extension Double: Testable {}

struct Foo<T: Testable> {}

// We need to grab Int's testable witness table.
let testableMetadata = reflect(Testable.self) as! ExistentialMetadata
let testable = testableMetadata.protocols[0]
var intTestable: WitnessTable? = nil

// Iterate through all of Int's conformances to find the Testable
// conformance.
for conformance in reflectStruct(Int.self)!.conformances {
  if conformance.protocol == testable {
    assert(!conformance.flags.hasGenericWitnessTable)
    intTestable = conformance.witnessTablePattern
  }
}

let fooMetadata = reflectStruct(Foo<Double>.self)!
let fooAccessor = fooMetadata.descriptor.accessor
// Foo<Int>
print(fooAccessor(.complete, (Int.self, intTestable)).type)

Parameters

  • request: MetadataRequest that instructs what state of metadata we want and whether we want to block or not. In most cases just ask for .complete.
  • args: A variadic parameter for including generic arguments and witness tables to types that have generic requirements and conformances in their type signature. This field MUST be correct for the function to work correctly.

Returns

MetadataResponse that has our requested metadata and tells us what state that metadata is in.

Types
Protocols
Global Variables
Global Functions
Clone this wiki locally