Just took a very brief look through Swift Syntax. I recall there have been special expressions like "#file
" that let you get out-of-band information. Looking through the various "...Syntax
" protocols, I saw one about macro expressions. Are all the old special expressions implemented as macros now (somehow), or is whatever is in the file is always interpreted as a macro call, then a later step differentiates between a built-in special expression vs. an actual macro call?
This proposal should answer some of your questions. In brief, yes, they’re interpreted by the compiler as freestanding expression macros. However, built-in macros like file, line, column, etc. get special treatment by the compiler so as to be expanded at the call site instead of the declaration site (as is customary for expression macros). The proposal I linked to aims to extend this capability to user-declared macros that are outside of the standard library.
Haven’t macros been part of Swift since 5.9?
You’re right, expression macros were implemented in 5.9 but the proposal text I linked to unlocks a new capability. The current implementation of expression macros always expands at the declaration site. The proposed change, however, is that when macros are used as default arguments, they’ll expand at the call site instead.