Let statement cannot use return

I am trying to understand the let statement. Particularly how to get variables from within the let scope.

julia> j = 2
2

julia> c,d = let a = j
           x = 5a
           y = 7a
           return x, y
           end
(10, 14)

julia> c
ERROR: UndefVarError: c not defined

If I simplify this a bit I get

julia> c = let a = j
           x = 5a
           return x
           end
10

julia> c
ERROR: UndefVarError: c not defined

Okay I think I figured it out, the return should not be there!

julia> c,d = let a = j
           x = 5a
           y = 7a
           x, y
           end
(10, 14)

julia> c
10

julia> d
14

I will post this anyway in case it helps someone else

Let isn’t a function body so returning jumps you out of the entire statement before the outermost assignment is evaluated. This could be an error but that makes stepping through code from a function body annoying.

4 Likes