Behavior of `local` in HS console is confusing
andmis opened this issue · comments
"local" is just basic Lua - it's not something Hammerspoon can change.
Have you read the "Getting Started" guide?
I think generally the local vs global scoping in Lua can be quite confusing, but the way I tend to think about it is local
is for temporary variables inside functions, and everything else should be global (unless it's in a config and I'm sure I only need something for a few consecutive lines, but generally I would put that in a function).
FWIW, even the official Lua REPL behaves this way:
❯ lua
Lua 5.4.4 Copyright (C) 1994-2022 Lua.org, PUC-Rio
> local x = 4
> x
nil
>
Also @andmis I feel bad that I've just immediately closed all of the issues you opened. I really appreciate people engaging with the project, so thank you for raising them 🙂
I have not read that guide cover to cover, maybe I should do that. Ok, looks like this is known and documented. Although "Hammerspoon can't do anything about this" is not true. But especially since this behavior matches that of the official REPL I guess it makes sense to leave it as-is.
Closing the issues is fine. 🙂
Very good point! It would be technically possible to make the Console have its own Lua scope, so that if you made something local in one command you can access it from another, but I feel like that might make the Console less helpful for debugging purposes. Unless there was something like a "Reset Console Scope" button so that you could essentially "Reload" the Console without reloading the main Lua configuration.
I think the existing functionality probably makes the most sense - but @cmsj might have some better insight into why it works the way it does. I'm pretty sure I asked a similar question when I first started using Hammerspoon!