typestar

Varargs and keywords in Julia

... collects arguments; keywords live after the semicolon.

# ... collects positional arguments; keywords sit after the ;.
function log_session(targets...; observer = "unattended", dome = "A")
    println("$(length(targets)) targets for $observer in dome $dome")
    for t in targets
        println("  - $t")
    end
end

log_session("M13", "M27")
log_session("Vega"; observer = "Rae")

# The splat also spreads a collection into a call.
queue = ["M81", "M82", "M101"]
log_session(queue...; dome = "B")

How it works

  1. targets... accepts any count of positional arguments.
  2. ; observer = "unattended" declares keyword defaults.
  3. Splatting spreads a vector into the call: log_session(queue...).

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
423
Tokens
97
Three-star pace
65 tpm

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

Type this snippet

Step 2 of 3 in Idioms, step 20 of 27 in Language basics.

← Previous Next →