Moderators IIYAMA Posted May 30, 2017 Moderators Share Posted May 30, 2017 Hi, I was curious when the garbage collector would clean something like this: function testFunction () local testVariable = 100 local function firstFunctionInBlock () print("first function, testVariable: " .. testVariable ) testVariable = testVariable + 1 end local function secondFunctionInBlock () print("second function, testVariable: " .. testVariable) testVariable = testVariable + 1 end return function () firstFunctionInBlock() secondFunctionInBlock () end end function testFunction ()-------------------------- -- function block -- --------------------------end Executed with: local newCreatedFunction = testFunction() newCreatedFunction() newCreatedFunction() newCreatedFunction() -- print("--") -- local newCreatedFunction2 = testFunction() newCreatedFunction2() newCreatedFunction2() newCreatedFunction2() Results: first function, testVariable: 100 second function, testVariable: 101 first function, testVariable: 102 second function, testVariable: 103 first function, testVariable: 104 second function, testVariable: 105 -- first function, testVariable: 100 second function, testVariable: 101 first function, testVariable: 102 second function, testVariable: 103 first function, testVariable: 104 second function, testVariable: 105 I assume when I nil the function that is active in the block. newCreatedFunction = nil It will clear the first created function block by the garbage collector. newCreatedFunction2 = nil Same goes for clearing the second created function block. I did like to hear your opinion about this matter! 3 Link to comment
Moderators IIYAMA Posted May 31, 2017 Author Moderators Share Posted May 31, 2017 Nobody is interested in it? I am getting really sad you know, eat my smiley! Link to comment
Syntrax# Posted May 31, 2017 Share Posted May 31, 2017 Hmm seems like an efficient method of using callbacks mate 1 Link to comment
pa3ck Posted May 31, 2017 Share Posted May 31, 2017 3 hours ago, IIYAMA said: Nobody is interested in it? I am getting really sad you know, eat my smiley! Well I just don't know what to answer, is it a question / suggestion or maybe both? 2 Link to comment
Scripting Moderators thisdp Posted May 31, 2017 Scripting Moderators Share Posted May 31, 2017 Lua will do that itself 1 Link to comment
Saml1er Posted May 31, 2017 Share Posted May 31, 2017 Nice! That's actually really interesting. For some reason, I was hoping testVariable might go out of scope when testFunction returns but then saw the results you posted and it was kinda surprising to me that it was not garbage-collected and remained in memory stack otherwise firstFunctionInBlock and secondFunctionInBlock would have failed to increment it. 1 Link to comment
Moderators IIYAMA Posted May 31, 2017 Author Moderators Share Posted May 31, 2017 It is indeed a kinda unexpected/ yet expected that testVariable remains part of the scope per function. But the main reason why I posted this, is: function testFunction () local testVariable = 100 local function firstFunctionInBlock () print("first function, testVariable: " .. testVariable ) testVariable = testVariable + 1 end local function secondFunctionInBlock () print("second function, testVariable: " .. testVariable) testVariable = testVariable + 1 end return function () firstFunctionInBlock() secondFunctionInBlock () end end This new created function is created inside the function block and yet it is the reason why this block remains to exist. If this function is destroyed it should automatic clear the whole function block with the garbage collector: Which has been created when the testFunction is called: local newCreatedFunction = testFunction() function testFunction () local testVariable = 100 local function firstFunctionInBlock () print("first function, testVariable: " .. testVariable ) testVariable = testVariable + 1 end local function secondFunctionInBlock () print("second function, testVariable: " .. testVariable) testVariable = testVariable + 1 end return function () firstFunctionInBlock() secondFunctionInBlock () end end 1 Link to comment
pa3ck Posted May 31, 2017 Share Posted May 31, 2017 Ah, I see what you were saying now, so when you call the function multiple times you expected the variable to be 100 again (set to default). I wonder if that's intended though, who knows. 1 Link to comment
Moderators IIYAMA Posted May 31, 2017 Author Moderators Share Posted May 31, 2017 no, I am not expecting that. I am expecting it to create a new copy of the default state of the block every time the function has been called. Which is lua actually doing at the moment. It is just strange that when the function (that has been created in testVariable) is destroyed, the block(copy) where it has been created also gets destroyed. I know it is complicated. Link to comment
Saml1er Posted May 31, 2017 Share Posted May 31, 2017 Right. A simple visual representation after the testFunction returns: local newCreatedFunction = function () -- or local function newCreatedFunction () -- testVariable, firstFunctionInBlock, secondFunctionInBlock still exist here for newCreatedFunction only! firstFunctionInBlock() secondFunctionInBlock () end Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now