Monday, October 17, 2022

A Possible Vision for Macros in Swift

Douglas Gregor (via Joe Groff):

Macros can offer a way out of this conundrum, because they allow one to introduce boilerplate-reducing facilities without requiring a bespoke language extension or a separate source-manipulating tool to do so. A good macro system could replace the need for a swath of new language features, but one must be careful not to degrade the development experience by making it too hard to build good tools (i.e., C macros are notorious for breaking tooling).

We propose to introduce a macro system into Swift. At a very high level, macros can be explicitly triggered with syntax such as #stringify(x + y), subsuming a number of existing expressions that use similar syntax already (#line, #colorLiteral(...), etc.) with a general features. Macros can also be triggered more implicitly in response to type checking, e.g., applying a macro to the argument passed to a function. Macro arguments are type-checked so that tooling behaves similarly to today, but macro evaluation involves transforming the syntax so that it can serve a number of different use cases.

A possible vision for macros in Swift talks about the design space and proposes a path for macros in Swift.


Comments RSS · Twitter

Leave a Comment