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

Should local at global scope immediately throw an error? #57483

Open
andyferris opened this issue Feb 20, 2025 · 0 comments
Open

Should local at global scope immediately throw an error? #57483

andyferris opened this issue Feb 20, 2025 · 0 comments
Labels
triage This should be discussed on a triage call

Comments

@andyferris
Copy link
Member

andyferris commented Feb 20, 2025

From a Hacker News comment today, this code at toplevel throws an error on line 4:

local s = 0

for i = 1:10
    t = s + i
    s = t
    println("$s")
end

println("$s")

Now I am not too surprised this doesn't work at toplevel, but it's a poor experience for users to encounter the error when reaching for s and not when declaring it. If the local var = declaration is guaranteed to be useless, then the code coming after is almost certainly going to have a bug.

IMO it would be a better user experience to throw an error on line 1, above, not line 4, since that is where the mistake in this code snippet is. Users will need to learn the global / non-global scoping rules to use Julia effectively, and the sooner we "teach" them the better (and clearly the user in the link above did not have a good experience).

@andyferris andyferris changed the title Should local at global scope immediate throw an error? Should local at global scope immediately throw an error? Feb 20, 2025
@oscardssmith oscardssmith added the triage This should be discussed on a triage call label Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage This should be discussed on a triage call
Projects
None yet
Development

No branches or pull requests

2 participants