Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Julia programmers need to prefix macros with the at-sign?

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.

like image 496
implicit_knowledge Avatar asked Mar 21 '15 12:03

implicit_knowledge


People also ask

What is a symbol in Julia?

This is the essence of a symbol: a symbol is used to represent a variable in metaprogramming. Once you have symbols as a data type, of course, it becomes tempting to use them for other things, like as hash keys. But that's an incidental, opportunistic usage of a data type that has another primary purpose.

What are macros in Julia?

Answer: Macros are sort of functions which take as input unevaluated expressions. ( Expr ) and return as output. another expression, whose code is then regularly evaluated at runtime. This post isn't a. substitute for reading the section about macros in the Julia.

What is metaprogramming in Julia?

Meta-programming is when you write Julia code to process and modify Julia code. With the meta-programming tools, you can write Julia code that modifies other parts of your source files, and even control if and when the modified code runs. In Julia, the execution of raw source code takes place in two stages.

What is quote in Julia?

This is referred to as quoting. The : character, followed by paired parentheses around a single statement of Julia code, produces an Expr object based on the enclosed code. Here is an example of the short form used to quote an arithmetic expression: julia> ex = :(a+b*c+1) :(a + b * c + 1) julia> typeof(ex) Expr.


1 Answers

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.

like image 153
Toivo Henningsson Avatar answered Oct 21 '22 03:10

Toivo Henningsson