Why would Julia programmers attribute macros with the at sign?

Whenever I see a Julia macro that is used as @assert or @time , I always wonder about the need to syntactically distinguish a macro with the @ prefix. What should I think about when using @ for macro? For me, this adds buzz and distraction to another very nice language (syntactically).

I mean, for me, β€œ@” has a link value, that is, a location such as a domain or address. In the sense of location @, it doesn't matter for macros other than this, this is another compilation step.

+5
source share
3 answers

@ should be considered a warning sign that indicates that normal language rules may not apply. For example, a function call

 f(x) 

will never change the value of the variable x in the calling context, and macro definition

 @mymacro x 

(or @mymacro f(x) , for that matter) is very good.

Another reason is that macros in Julia are not based on textual substitution, like C, but on substitution in an abstract syntax tree (which is much more powerful and avoids the unexpected consequences that text substitution macros print).

Macros have special syntax in Julia, and since they expand after parsing time, the parser also needs an unambiguous way to recognize them (not knowing which macros were defined in the current area).

ASCII characters are a valuable resource in the design of most programming languages, including Julia. I would suggest that choosing @ basically comes down to not being needed for something more important and that it looks great.

+14
source

Symbols should always be interpreted in the context in which they are used. Having multiple values ​​for characters in different contexts is not new and will probably never disappear. For example, no one should expect #include in C to be a virus on Twitter.

Julia User Guide Delay: why are macros? pretty well explains some of the things you can keep in mind when writing and / or using macros.

Here are some snippets:

Macros are necessary because they are executed during code analysis; therefore, macros allow the programmer to generate and include fragments of custom code before running the full program.

...

It is important to emphasize that macros get their arguments as expressions, literals, or characters.

So, if a macro is called with an expression, it gets the whole expression, not just the result.

...

Instead of written syntax, the macro call expands upon analysis to the time it returns.

+2
source

This really works well with the semantics of the @ character.

If we look at the Wikipedia entry for the symbol β€œAt”, 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 relationship.

Due to the fact that we can use the @ -symbol as a shorthand for the preposition on, to refer to the spatial connection, that is, in a place like @tony bar, @france, etc., in some @ 0x50FA2C memory location (for example , for pointers / addresses), to the recipient of the message (@ user0851, who use Twitter and other forums, etc.), but also for temporary communication, i.e. @ 05:00 a.m., @midnight, @compile_time or @parse_time .

And since macros are processed at the time of parsing (here you have it), and this is completely different from other code that is evaluated at runtime (yes, there are many different phases between them, but not here). In addition to direct attention to the programmer, the following code fragment is processed during parsing! unlike runtime, we use @.

For me, this explanation fits perfectly into the language.

thanks @all;)

+1
source

Source: https://habr.com/ru/post/1215813/


All Articles