Skip to content

allow replacing const binding with new binding? #30277

Closed
@StefanKarpinski

Description

@StefanKarpinski

People are often quite confused and frustrated by this sort of thing: JuliaAcademy/JuliaTutorials#27. The core issue is this:

julia> sum(rand(10)) # resolves `Main.sum` to be `Base.sum`
4.042512623756739

julia> sum = 3 + 4
ERROR: cannot assign variable Base.sum from module Main
Stacktrace:
 [1] top-level scope at none:0

This also crops up when some bit of machinery accidentally and mistakenly resolves bindings that you didn't even use, e.g. #30234, #23677, #29962, etc. But the end result is the same: frustration that one cannot use a particular name without restarting Julia. And this is not limited to newbies: if I'm debugging code that uses e.g. sum any other function name as a local variable—which is fine in local scope—then I have to either start a new REPL and be very careful not to use that function before then or I have to change the name throughout the code.

To that end, I would propose this relaxation of the current behavior: if someone assigns to an already-bound constant, we print a warning along these lines:

WARNING: cannot assign variable Base.sum from module Main, creating a new binding instead

or in the case of a plain const with an incompatible assignment:

WARNING: invalid redefinition of constant c, creating a new binding instead

Any function definition that used a previous binding would continue to use that binding and be unaffected by the new one, so no recompilation would be required. I think this would make this behavior feel like less of a mine field to navigate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    julepJulia Enhancement ProposalmodulesspeculativeWhether the change will be implemented is speculative

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions