Skip to content

Commit ecc6da5

Browse files
author
Tomas Pevny
committed
fixing
1 parent 19007be commit ecc6da5

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

docs/src/lectures/lecture_07/lecture.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -225,46 +225,44 @@ end
225225
The error code snippet errors telling us that the expression `"$"` is outside of a quote block. This is because the macro `@no_quote` has returned a block with `$` occuring outside of `quote` or string definition.
226226

227227
::: info
228-
Some macros like `@eval` (recall last example)
229-
```julia
230-
for f in [:setindex!, :getindex, :size, :length]
231-
@eval $(f)(A::MyMatrix, args...) = $(f)(A.x, args...)
232-
end
233-
```
234-
or `@benchmark` support interpolation of values. This interpolation needs to be handled by the logic of the macro and is not automatically handled by Julia language.
228+
Some macros like `@eval` (recall last example)
229+
```julia
230+
for f in [:setindex!, :getindex, :size, :length]
231+
@eval $(f)(A::MyMatrix, args...) = $(f)(A.x, args...)
232+
end
233+
```
234+
or `@benchmark` support interpolation of values. This interpolation needs to be handled by the logic of the macro and is not automatically handled by Julia language.
235235
:::
236236

237237
Macros do not know about runtime values, they only know about syntax trees. When a macro receives an expression with a `$x` in it, it can't interpolate the value of x into the syntax tree because it reads the syntax tree before `x` ever has a value!
238238

239239
Instead, when a macro is given an expression with `$` in it, it assumes you're going to give your own meaning to `$x`. In the case of BenchmarkTools.jl they return code that has to wait until runtime to receive the value of `x` and then splice that value into an expression which is evaluated and benchmarked. Nowhere in the actual body of the macro do they have access to the value of `x` though.
240240

241241

242-
::: info
243-
### Why `$` for interpolation?
244-
The `$` string for interpolation was used as it identifies the interpolation inside the string and inside the command. For example
245-
```julia
246-
a = 5
247-
s = "a = $(a)"
248-
typoef(s)
249-
println(s)
250-
filename = "/tmp/test_of_interpolation"
251-
run(`touch $(filename)`)
252-
```
242+
::: info Why `$` for interpolation?
243+
The `$` string for interpolation was used as it identifies the interpolation inside the string and inside the command. For example
244+
```julia
245+
a = 5
246+
s = "a = $(a)"
247+
typoef(s)
248+
println(s)
249+
filename = "/tmp/test_of_interpolation"
250+
run(`touch $(filename)`)
251+
```
253252
:::
254253

255254
## [Macro hygiene](@id lec7_hygiene)
256255
Macro hygiene is a term coined in 1986 addressing the following problem: if you're automatically generating code, it's possible that you will introduce variable names in your generated code that will clash with existing variable names in the scope in which a macro is called. These clashes might cause your generated code to read from or write to variables that you should not be interacting with. A macro is hygienic when it does not interact with existing variables, which means that when macro is evaluated, it should not have any effect on the surrounding code.
257256

258257
By default, all macros in Julia are hygienic which means that variables introduced in the macro have automatically generated names, where Julia ensures they will not collide with user's variable. These variables are created by `gensym` function / macro.
259258

260-
::: info
261-
### gensym
262-
263-
`gensym([tag])` Generates a symbol which will not conflict with other variable names.
264-
```julia
265-
julia> gensym("hello")
266-
Symbol("##hello#257")
267-
```
259+
::: info gensym
260+
`gensym([tag])` Generates a symbol which will not conflict with other variable names.
261+
```julia
262+
julia> gensym("hello")
263+
Symbol("##hello#257")
264+
```
265+
:::
268266

269267
Let's demonstrate it on our own version of an macro `@elapsed` which will return the time that was needed to evaluate the block of code.
270268
```julia

0 commit comments

Comments
 (0)