8

Whenever I see a Julia macro in use like @assert or @time I'm always wondering about the need to distinguish a macro syntactically with the @ prefix. What should I be thinking of when using @ for a macro? For me it adds noise and distraction to an otherwise very nice language (syntactically speaking).

I mean, for me '@' has a meaning of reference, i.e. a location like a domain or address. In the location sense @ does not have a meaning for macros other than that it is a different compilation step.

  • 6
    You should be thinking "hey, I'll try `macroexpand` or read any relevant docs to figure out what this macro expands to, because otherwise I have no idea what this code will do!". It is an explicit design decision to give macro invocations a special syntax so that users realize something different is happening. I don't think there is much more to the "meaning" of at-sign in particular, other than that at-sign happened to be available (if you think this usage is confusing, check out PEP 465). – Isaiah Norton Mar 21 '15 at 16:48
  • 1
    Thanks, after looking up the Wikipedia article the semantics of the @ symbol as a temporal preposition fits quite nicely! – implicit_knowledge Mar 21 '15 at 21:13

3 Answers3

18

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.

Toivo Henningsson
  • 2,689
  • 12
  • 10
4

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.

rickhg12hs
  • 10,638
  • 6
  • 24
  • 42
2

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 ;)