Metaprogramming

Nicolas Jacquin

Sometimes, we need to work with the code syntax directly, treating it as data. This comes up often for logging, benchmarking and monitoring (think of @time or @btime from BenchmarkTools and @showprogress from ProgressMeter), adding extra features to existing code without rewriting it (think of @threads, @async), changing the meaning of some code structures (with @assert to make something a unit test) or cheeky compiler tricks (@turbo, which promises the compiler your code doesn’t index out of bounds, iterate over empty collections, order doesn’t matter, etc…)

We want to write things like this: @log_step x + y * z

And get outputs like this: [LOG] x + y * z = 42

This is behaviour that’s difficult to emulate with a function.

Downloadable content

Notebook: julia | html | pdf