Skip to content

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

Open
@andyferris

Description

@andyferris

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:loweringSyntax lowering (compiler front end, 2nd stage)minor changeMarginal behavior change acceptable for a minor releaseparserLanguage parsing and surface syntax

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions