You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/lectures/lecture_07/lecture.md
+24-26Lines changed: 24 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -225,46 +225,44 @@ end
225
225
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.
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.
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.
235
235
:::
236
236
237
237
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!
238
238
239
239
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.
240
240
241
241
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
+
```
253
252
:::
254
253
255
254
## [Macro hygiene](@id lec7_hygiene)
256
255
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.
257
256
258
257
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.
259
258
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
+
:::
268
266
269
267
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.
0 commit comments