-
-
Notifications
You must be signed in to change notification settings - Fork 37
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
Rethink var declaration rules? #11
Comments
This is a tricky problem... it boils down to should writes to variables have the same name lookup as variable reads. For variable reads, I think it's pretty favorable to look upwards until a reference is found. It makes closures easier to write and easier to use global variables/functions. So then for variable writes, if it favors locals first, it means you'd need extra syntax like python. I'm somewhat ok with that as I like the idea of not having an extra keyword for locals and not worrying about variables leaking. But it would require users to keep track of two different behaviors when dealing with variables. And what if you wanted to do a simple write in a closure, would that also need additional syntax? Another twist to consider in Cyber is that functions have their own scope but subblocks within that function do not. So ifs, loops, they all share the same variable scope as the function. |
For me it's more a matter of declaration and assignment having the same syntax. (Which is something I already prefer to avoid, 'cause I make typos.) That combines with assignment's context-sensitivity to make the surprising behavior possible. For variable reads, this is fine because you can't sneakily screw something up by reading the wrong variable. Outside the current function, anyway. (Reading undeclared variables returning "none" is a different issue.)
Not 100% sure, but I think you would need something like this anyway. One way or another you need to be able to tell it whether you are referring to something inside or outside of the local scope. Python has |
I think you're right. Leaking writes is just far worse than leaking reads. Here's what I propose:
|
Here's another relevant example that currently has behavior I find extremely confusing:
The output here is With regards to your proposal to resolve this, I think that sounds very reasonable though I would propose replacing the keyword I also agree with @icefoxen with regards to declaration and assignment using the same syntax being a source of bugs in general. Consider the following example which prints 42:
|
I like |
Hm, this discussion reminds me a little of the way one needs to handle variables in Tcl. It is not the most user friendly nor safe way... But I am not against any solutions in this space unless it would require variable shadowing effectively disallowing implementation of #26 . |
One reason I'm conflicted with having a separate local declaration is that doing it in sub-blocks can also be confusing due to the property that every variable in a function belongs to the same scope. This property came about from the need to reduce the amount of refcounting and to utilize the same reserved virtual register as much as possible. So in this example, it would be allowed but it's confusing:
Another reason is that I think baking a language construct to prevent a misspelling (in ifreund's case) is not as justifiable in a scripting language. This can be solved by an editor with autocomplete. Thirdly, one of the goals in Cyber is to reduce the number of compile errors or any (stop what you're doing, you can't evaluate the rest of the code until this one thing is fixed). |
This has been implemented. Please open another issue if you have concerns about the new declaration rules. |
Stumbled across the language website from lobste.rs and thought the language was pretty cool! One comment though, considering the test program:
This prints "234" "none", so
a
is a local variable being declared.If you change it to:
It prints "234" "234", so
a
is a variable from the enclosing scope, being assigned. There's two very different operations here that look identical, and differ only in context that may be 1000 lines of code away from where it matters.This seems like a pretty good footgun, since it breaks locality. There's no way to look at
func foo()
alone and tell whether or not it changes state outside of it. Lua's "every variable is a global unless declared otherwise" has similar effects and is IMO a pretty good example of how much of a PITA this can be, so I humbly beg you to consider whether this is really what you want.Thanks!
The text was updated successfully, but these errors were encountered: