I'm not a lua expert, so I can be totally mistaken on this - please correct me if so.
I can't see any reason why a variable should be global in pico-8... Defining as a local outside any functions, makes it accessible from that file (or module? or what is the correct lua terminology for this?) and since pico-8 only has one code file, that is essentially makes it global.
And as far as I know locals are way faster in lua than globals.
So I suggest just removing the keyword, and making everything local by default.
If there is a reason for globals, I would still ask for local-by-default, and suggest introducing a "global" keyword - just to save on the code size.
Again, I don't know much about the inner workings of lua/pico-8, so just ignore this if it doesn't make any sense. :)
Wouldn't local by default cause problems with scope in cases like this?
a = 1 if true then -- If local is implied, this would be declaring a new "a" in -- this little block's scope a = 2 end print(a) -- This would output "1"
Since it would be the same as
local a = 1 if true then local a = 2 end print(a) -- This outputs "1"
It's ambiguous whether you wanted to assign to the existing "a" or declare a new one, since now the syntax is the same for both...I guess it could work like globals do where if the variable already exists, it uses it instead of declaring a new one, but it seems like it would be a rather large change to the way Lua works, and not just a simple syntactical find/replace type of change (thus not likely to be implemented).
Well, good point...
Thinking some more on it, it is probably not really a problem of scoping global/local, but the lack of syntax for variable definition.
In your example (and in lua in general) "a=1" means two different things - either define the variable, or(and) assign the value the already defined one.
So maybe the solution is using a syntax specifically to defining a variable... but I guess that would be pretty much the same as using the local keyword... :)
So, yeah, I guess this suggestion can be safely ignored :)
Or maybe I just modify it to changing the "local" keyword to "var" with the same meaning - no change in the interpretation, but saves two characters on the code size at every variable. :)
> Or maybe I just modify it to changing the "local" keyword to "var" with the same meaning - no change
> in the interpretation, but saves two characters on the code size at every variable. :)
I suggested this same thing in a different thread :D
EDIT: and yes, you're right; I think I got a little confused and the issue is really not scope per se. And then I edited my answer right before you replied which I probably shouldn't have done XD
[Please log in to post a comment]