Skip to content

UndefVarError if trying to shadow global variable using let #50369

Closed
@jonas-schulze

Description

@jonas-schulze

When working with Pluto, I like to wrap more complex cells using let and reuse simple variable names.
Within Pluto, everything works fine, but when executing the notebook as a standalone script, Julia complains about some variables not being defined.

MWE

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.9.1 (2023-06-07)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> x = 5
5

julia> y = let
           x = 2x
           x += 1
       end
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope
   @ REPL[2]:2

Workaround

By removing the initial line break, Julia gets what I mean.
But I think this is a bug of Julia (and I don't want to use different variable names or edit the whole already-too-long notebook). 🙃

julia> y = let x = 2x
           x += 1
       end
11

julia> x
5

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions