A Possible Vision for Macros in Swift

As someone who generally avoids macros like these but would like compile-time meta-programming, let me give you the perspective of the macro-averse (largely because I avoid domains that require boilerplate to begin with)

I want to explain a bit what I meant by this. In my experience, there are at least four common uses of compile-time meta-programming:

  1. Compile-time Code Generation (CTCG): the syntactic approach in this gist is clearly aimed primarily at this usecase, largely aimed at reducing boilerplate.
  2. Compile-time Manipulation (CTM): examples would be conditional compilation through #if statements or type aliases, or perhaps some day allowing the programmer to set data alignment to compile-time computed values.
  3. Compile-time Code Execution (CTCE): This is the least important, as you can always manually run the same code and output the results to build inputs, but when needed for the first two usecases above, it makes them so much easier to use.
  4. Generics: Really just a combination of the first three, but so important that it was implemented first with its own DSL.

The syntactic macro approach linked here may be sufficiently powerful that you could implement all of these with it, but it is primarily suited for CTCG, ie you probably wouldn't want to re-implement generics with it. I am primarily interested in CTM and some CTCE to help with it, which I don't think this approach is really geared towards. Not knocking this approach, as I see the need for it for CTCG, but I would like to see further work on the other two usecases in time.

3 Likes