Initialize multiple variables in one line

The proposed solutions still seem to violate DRY (a bit :wink:) because the number of the variables on the left-hand side of the assignment must match the number on the right-hand side. Unfortunately I don’t see a way to fix this without a macro. Perhaps the following:

macro multidef(ex)
   @assert(ex isa Expr)
   @assert(ex.head == :(=))
   vars = ex.args[1].args
   what = ex.args[2]
   rex = quote end
   for var in vars
      push!(rex.args, :($(esc(var)) = $what))
   end
   rex
end

It can be used as follows:

@multidef a, b, c = Set{Symbol}()

Which has the right outcome:

julia> a,b,c
(Set(Symbol[]), Set(Symbol[]), Set(Symbol[]))

julia> a===b
false
6 Likes