I don't get why this goes out of scope #3441
-
sharedwatchers.lua:
If requiring this module early in init.lua, the timer variable immediately goes out of scope and never triggers. Why is there a difference? Why would a local module-level variable ever go out of scope? I don't know if it's just me, but the scope of variables and GC in Lua is a chore to wrap your head around. init.lua: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
In a seeming paradox, the busier your lua code is (i.e. how much there is to evaluate), the quicker garbage collection occurs. This is why putting it later in your I discuss local variables and scope in this wiki article I wrote a while back... I need to go through it again to clean it up and add some more examples, before adding it to the official Wiki, but hopefully even in its current state, it will help your understanding: https://github.com/asmagill/hammerspoon/wiki/Variable-Scope-and-Garbage-Collection |
Beta Was this translation helpful? Give feedback.
In a seeming paradox, the busier your lua code is (i.e. how much there is to evaluate), the quicker garbage collection occurs. This is why putting it later in your
init.lua
seems to work -- if you do more in the console or have a very active setup with lots of timers or callbacks, eventually it will be collected.I discuss local variables and scope in this wiki article I wrote a while back... I need to go through it again to clean it up and add some more examples, before adding it to the official Wiki, but hopefully even in its current state, it will help your understanding: https://github.com/asmagill/hammerspoon/wiki/Variable-Scope-and-Garbage-Collection