Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make variables defined in try visible from else #46928

Closed
jariji opened this issue Sep 27, 2022 · 5 comments
Closed

Make variables defined in try visible from else #46928

jariji opened this issue Sep 27, 2022 · 5 comments
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@jariji
Copy link
Contributor

jariji commented Sep 27, 2022

In a try/else I usually want to refer to a variable defined in the try but it's not available unless I put local x before the try.

julia> try
         x = 1
       catch 
         nothing
       else
         println(x)
       end
ERROR: UndefVarError: x not defined
Stacktrace:
 [1] top-level scope
   @ REPL[1]:5
@petvana
Copy link
Member

petvana commented Sep 29, 2022

I'd like to mention that try/catch/else seems undocumented now in Julia. The implementation comes from #42211. Cc: @simeonschaub

@petvana
Copy link
Member

petvana commented Sep 29, 2022

Btw, Python's docs provides an explicit example of a new local variable f

for arg in sys.argv[1:]:
    try:
        f = open(arg, 'r')
    except OSError:
        print('cannot open', arg)
    else:
        print(arg, 'has', len(f.readlines()), 'lines')
        f.close()

@jariji
Copy link
Contributor Author

jariji commented Sep 29, 2022

In Python the variable escapes all the way, which we might not want.

In [2]: try:
   ...:     x = 1
   ...: except:
   ...:     pass
   ...: 

In [3]: x
Out[3]: 1

@JeffBezanson JeffBezanson added the compiler:lowering Syntax lowering (compiler front end, 2nd stage) label Sep 30, 2022
@IanButterworth
Copy link
Member

@simeonschaub bump on this, and the documentation.

I tried to come up with a definitive example for the docs to PR but couldn't decide on a brief one.

@simeonschaub
Copy link
Member

try, catch and finally have always introduced new scope blocks, so this is too late to change now without causing silently wrong results in existing code. Declaring x local to the surrounding scope is the correct way to achieve this.

I will try to come up with some docs for this

simeonschaub added a commit that referenced this issue Jan 26, 2023
simeonschaub added a commit that referenced this issue Jan 26, 2023
ref #46928

Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com>
KristofferC pushed a commit that referenced this issue Feb 1, 2023
ref #46928

Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com>
(cherry picked from commit d775750)
KristofferC pushed a commit that referenced this issue Feb 6, 2023
ref #46928

Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com>
(cherry picked from commit d775750)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

5 participants