[Returned for Revision] SE-0385: Custom Reflection Metadata

Hello Swift Community,

The review of SE-0385 "Custom Reflection Metadata" ran from January 24, 2023 through February 7, 2023. There was significant interest in the capabilities provided by the proposal, as well as concerns about its current design. The proposal is returned for revision.

The Language Workgroup agrees with the motivation and general direction of the proposal, to provide opt-in annotations that create user-specified metadata that can be queried at runtime. The proposal itself provides important use cases, and additional ones were discussed as part of the review thread, to sufficiently motivate inclusion in the language.

A number of issues came up in the review that should be addressed by a revision:

  • The use of ad hoc overloading of init(attachedTo:) initializers, which requires separate overloads for the different kinds of entity (properties, types, functions, etc.), introduces a number of limitations with no clear path toward generalization. The approach favors entities with direct corollaries in the runtime, such a concrete types (described by a metatype instance) and properties (described by a key path). Other kinds of entities, such as generic types and extensions, are not expressible in this design. Custom metadata for functions fell somewhere in between: it is expressible with function values, but with significant compromises, including the need to add many overloads of init(attachedTo:) for different function signatures, and the loss of important information like argument labels. The use of custom attributes in the proposal implies that macros may provide a better mechanism for creating metadata for a specific declaration, because they are more amenable to ad hoc processing of all kinds of declarations.
  • The runtime discovery mechanism relies on executing generator functions to produce run-time values for custom attribute metadata. This design precludes static extraction of custom metadata, such as a tool that extracts the set of available tests from a binary. The design should consider whether the generated metadata could be made more amenable to static extraction, as well as more efficient queries, by considering the interaction with constant initialization.
  • The runtime discovery APIs are dependent on a reflection library that itself is undergoing a number of design changes. While it is useful to consider the language feature proposed here, the direction of the reflection library will need to be more clear before this proposal can build on top of it.

The Language Workgroup would like to see a revised proposal with a design that considers composition with macros and constant initialization and addresses the above concerns.

Doug Gregor
Review Manager


Is there a place to learn about the design changes going into the reflection library?

While I am a little bummed out that this proposal did not go through, but I appreciate the additional requirements. Not sure where one can participate to discuss potential solutions for the additional requirements outlined here

1 Like