let
is great and very important to have, but the syntax is very unusual.
Before running them, try to guess what these do:
function f1()
a = 1
b = 2
c = let a = 3
b = 4
a+b
end
(a,b,c)
end
function f2()
a = 1
b = 2
c = let
a = 3
b = 4
a+b
end
(a,b,c)
end
function f3()
a = 1
b = 2
c = let begin
a = 3
b = 4
end
a+b
end
(a,b,c)
end
f1
is the standard usage of let
. But the only new binding is for a
! This is very surprising for anyone coming from languages with let...in
syntax. There, we’d expect the value of the expression to be the last line in the block. This is not the case.
f2
looks like it ought to be equivalent, but it’s not! This is one of the very few cases where Julia is whitespace-sensitve.
f3
seems like it would be a sensible way around this limitation, but it doesn’t even parse!
As a result of this syntax, there are lots of examples in the wild of people using let
where it doesn’t do anything let
-like.
Why is let
designed this way? Can it change for Julia 2.0?