MultiAssign.jl

Assign the same value/expression to multiple variables with one terse and clear line
Author markmbaum
Popularity
1 Star
Updated Last
3 Years Ago
Started In
November 2021

MultiAssign.jl

Build Status Coverage

MultiAssign exports a macro for assigning a value/expression to multiple variables in a single terse and clear line. For example, you can write

@multiassign x, y, z = zeros(n)

instead of

x = zeros(n)
y = zeros(n)
z = zeros(n)

or

x, y, z = (zeros(n) for _ in 1:3)

Importantly, each of the variables is assigned with a distinct call to zeros in the example above. The resulting x, y, and z vectors are not copies of each other.

The macro should work wherever multiple variables should be assigned the same value. It simply generates identical assignments for each variable.

Type annotations will also work if you need them, except in global scope. For example

@multiassign a::Float64, b::Float32, c::Int64 = 0

will work inside a function, but not at the global scope of the REPL.