January 2020
Intermediate to advanced
532 pages
13h 31m
English
Let's start with a simple one. Suppose that we want to create a macro called @squared that takes an expression and just squares it. In other words, if we run @squared(x), then it should be translated into x * x:
macro squared(ex) return :($(ex) * $(ex))end
At first glance, it seems to work fine when we run it from the REPL:

But this macro has a problem with the execution context. The best way to illustrate the problem is by defining a function that uses the macro. So let's define a foo function, as follows:
function foo() x = 2 return @squared xend
Now, when we call the function, we get the following error: ...
Read now
Unlock full access