typestar

Macros in Julia

@show, @assert and @time — the everyday three.

subs = [12, 18, 9]

# @show prints the expression and its value -- the field notebook.
@show sum(subs)
@show length(subs) * 5

# @assert documents an invariant and fails loudly when it breaks.
@assert all(subs .> 0) "every stack needs at least one sub"

# @time measures a computation right where it runs.
@time total = sum(x^2 for x in 1:10_000)
println("Sum of squares: $total")

How it works

  1. @show expr prints the expression text and its value.
  2. @assert cond msg fails loudly when the invariant breaks.
  3. @time measures allocation and elapsed time in place.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
380
Tokens
56
Three-star pace
65 tpm

At the three-star pace of 65 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 1 of 3 in Macros & formatting, step 22 of 27 in Language basics.

← Previous Next →