One question has been continually bothering me—why do we have this syntax let x = 1, y = 2?
Since we used to write x, y = 1, 2 directly in julia REPL.
Otherwise it’s invalid as follows
julia> x = 1, y = 2
ERROR: syntax: invalid assignment location "1" around REPL[7]:1
Stacktrace:
[1] top-level scope
@ REPL[7]:1
Then why do we need to invent the additional style x = 1, y = 2 and exclusively associate it to let?
My intuition was acute to find that we have this Cartesian For-loop usage.
for i = 1:2, j = 1:3
And this fact
julia> for i = 1 #
ERROR: ParseError:
# Error @ REPL[2]:2:2
for i = 1 #
#└ ── premature end of input
Stacktrace:
[1] top-level scope
@ none:1
Note that this ParseError is not as severe as the one let x = 1 # has, because the latter cannot tolerate an Enter stroke, whereas the former can.
You’re forgetting about the = in () syntax of named tuples and keyword arguments in function calls, though the latter parses to :kw subexpressions instead of :(=).
Yes, I do forget them. Because I hardly use them.
I only write normal functions function f(a, b ,c) in my daily code.
And I almost never use named tuples either.
Seems I understand now. let/for can create a dependence tree, whereas x, y = 1, 2 style is not.
for i = 1:3, j = 1:i, k = 1:i+j
# some body
end
julia> let x = 3, y = 4, x = y, y = x
println("x = $x, y = $y")
end
x = 4, y = 4
julia> let x = 3, y = 4, (x, y) = (y, x)
println("x = $x, y = $y")
end
x = 4, y = 3
Is there a good reason that the second style is not supported?
julia> for _ = 1:3
local x, y = 7, 8
println("x = $x, y = $y")
end
x = 7, y = 8
x = 7, y = 8
x = 7, y = 8
julia> for _ = 1:3
local x = 7, y = 8
println("x = $x, y = $y")
end
ERROR: syntax: invalid assignment location "7" around REPL[2]:2
Stacktrace:
[1] top-level scope
@ REPL[2]:1