Why do Julia programmers need to prefix macros with the at-sign?
The @
should be seen as a warning sign which indicates that the normal rules of the language might not apply. E.g., a function call
f(x)
will never modify the value of the variable x
in the calling context, but a macro invocation
@mymacro x
(or @mymacro f(x)
for that matter) very well might.
Another reason is that macros in Julia are not based on textual substitution as in C, but substitution in the abstract syntax tree (which is much more powerful and avoids the unexpected consequences that textual substitution macros are notorious for).
Macros have special syntax in Julia, and since they are expanded after parse time, the parser also needs an unambiguous way to recognise them (without knowing which macros have been defined in the current scope).
ASCII characters are a precious resource in the design of most programming languages, Julia very much included. I would guess that the choice of @
mostly comes down to the fact that it was not needed for something more important, and that it stands out pretty well.
Symbols always need to be interpreted within the context they are used. Having multiple meanings for symbols, across contexts, is not new and will probably never go away. For example, no one should expect #include
in a C program to go viral on Twitter.
Julia's Documentation entry Hold up: why macros? explains pretty well some of the things you might keep in mind while writing and/or using macros.
Here are a few snippets:
Macros are necessary because they execute when code is parsed, therefore, macros allow the programmer to generate and include fragments of customized code before the full program is run.
...
It is important to emphasize that macros receive their arguments as expressions, literals, or symbols.
So, if a macro is called with an expression, it gets the whole expression, not just the result.
...
In place of the written syntax, the macro call is expanded at parse time to its returned result.
It actually fits quite nicely with the semantics of the @
symbol on its own.
If we look up the Wikipedia entry for 'At symbol' we find that it is often used as a replacement for the preposition 'at' (yes it even reads 'at'). And the preposition 'at' is used to express a spatial or temporal relation.
Because of that we can use the @
-symbol as an abbreviation for the preposition at to refer to a spatial relation, i.e. a location like @tony's bar, @france, etc., to some memory location @0x50FA2C (e.g. for pointers/addresses), to the receiver of a message (@user0851 which twitter and other forums use, etc.) but as well for a temporal relation, i.e. @05:00 am, @midnight, @compile_time or @parse_time.
And since macros are processed at parse time (here you have it) and this is totally distinct from the other code that is evaluated at run time (yes there are many different phases in between but that's not the point here). In addition to explicitly direct the attention to the programmer that the following code fragment is processed at parse time! as oppossed to run time, we use @.
For me this explanation fits nicely in the language.
thanks@all ;)