You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, redeclaring a variable from const to var and trying to assign to it causes the variable to sort of "remember" it was a const variable before, therefore preventing the assignment, even though it's not a constant anymore. This problem only happens with global variables. A possible solution would be to check for redeclaration and delete the identifier from the constants table, thus allowing assignment later to it.
Related to this, importing from a file that declared global constants in the REPL brings this issue of declaration, because in the REPL the constants table isn't freed after every compilation.
Expected Behavior
The expected behaviour should be to allow the redeclaration into var and allowing assignment, or not allowing redeclaration entirely.
Steps To Reproduce
Declare a const variable globally
Try to redeclare it as var
Assign to the redeclared variable
Anything else?
No response
The text was updated successfully, but these errors were encountered:
Yeah this is one that I've always questioned, it actually comes as a design decision from Lox:
This code doesn’t check to see if the key is already in the table. Lox is pretty lax with global variables and lets you redefine them without error. That’s useful in a REPL session, so the VM supports that by simply overwriting the value if the key happens to already be in the hash table.
If we are going to error this out, it'd be good to pick it up at compile time and even potentially switch the internals of the global variable to be the same as how local variables work
Completely agree. I wrote runtime but meant compile time. Given the amount of rope that scripting languages provide, so to speak, it's nice to know there's a safe guard like this.
Is there an existing issue for this?
Current Behavior
Currently, redeclaring a variable from
const
tovar
and trying to assign to it causes the variable to sort of "remember" it was aconst
variable before, therefore preventing the assignment, even though it's not a constant anymore. This problem only happens with global variables. A possible solution would be to check for redeclaration and delete the identifier from the constants table, thus allowing assignment later to it.Related to this, importing from a file that declared global constants in the REPL brings this issue of declaration, because in the REPL the constants table isn't freed after every compilation.
Expected Behavior
The expected behaviour should be to allow the redeclaration into
var
and allowing assignment, or not allowing redeclaration entirely.Steps To Reproduce
const
variable globallyvar
Anything else?
No response
The text was updated successfully, but these errors were encountered: