This is something I’ve always wondered… Why does a let
block allow comma-separated assignments?
let x = 1, y = 2, z = 3
(x, y, z)
end
As far as I can tell, this is completely identical to
let x = 1; y = 2; z = 3
(x, y, z)
end
or
let
x = 1
y = 2
z = 3
(x, y, z)
end
This also doesn’t seem (?) to change the scoping rules:
a = 1
let x = a
x
end
# equivalent to
let
x = a
x
end
Furthermore, let
evaluates the expressions in order, so you can do
let x = 1, y = x + 1, z = y + 1
(x, y, z)
end
which is actually the same as if you had ;
-separated expressions. So… why not just use ;
here, rather than using the alternate behavior of ,
?
I find this a bit strange because in a begin
block, or simply typed to the REPL, the expression x = 1, y = 2, z = 3
is invalid. It’s also weird because (x = 1, y = 2, z = 3)
normally instantiates a named tuple, rather than assigning variables (or for defining default arguments of a function).
Is this some vestigial syntax from earlier Julia versions? Or perhaps I am missing something subtle about the scoping rules here?