Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: Skip variable declaration

Tags:

lua

I am trying to "Skip" a variable, by either never declaring it or just having it garbage collected immediately, but I don't know if it's possible.

Example:

function TestFunc()
   return 1, 2
end

function SecondFunction()
   local nodeclare, var = TestFunc()
end

Basically what I wanted was for "nodeclare" to not even exist. So if I did print(nodeclare, var) it would do nil, 2. The same thing would be if I was doing a pairs loop and I didn't need to use the keyvalue. Is there some special thing I can put as the variable name for this to happen? If say I was doing a pairs loop over 100 values, would that even have a signifigant impact?

like image 784
user2671497 Avatar asked Feb 27 '14 20:02

user2671497


People also ask

What are the types of variables in Lua?

This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.

What is BREAK statement in Lua Lua?

Lua - break Statement. When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. If you are using nested loops (i.e., one loop inside another loop), the break statement will stop execution of the innermost loop and start executing...

What is a to-be-closed variable in Lua?

Like a to-be-closed variable in Lua, the value at that slot in the stack will be closed when it goes out of scope. Here, in the context of a C function, to go out of scope means that the running function returns to Lua, or there is an error, or the slot is removed from the stack through lua_settopor lua_pop, or there is a call to lua_closeslot.

What happens if you call a non valid function in Lua?

For functions that can be called with acceptable indices, any non-valid index is treated as if it contains a value of a virtual type LUA_TNONE, which behaves like a nil value. 4.1.3 – Pointers to strings Several functions in the API return pointers (const char*) to Lua strings in the stack.


1 Answers

First of all, variables are not garbage collected, objects are. In this case, there's nothing to garbage collect.

However, let's say that TestFunc was creating objects (say, tables):

function TestFunc()
   return {1}, {2}
end

function SecondFunction()
   local nodeclare, var = TestFunc()
end

Now nodeclare is referencing a table returned by TestFunc. That's an object, allocated on the heap, that we don't want hanging around forever.

That object will eventually be collected if there is nothing left referring to it. In your case, as soon as SecondFunction returns, the local nodeclare goes out of scope and goes away. As long as there's nothing else referencing that table, the table will be collected (during next collection cycle).

You can avoid declaring nodeclare entirely by skipping the first return value of TestFunc like this:

local var = select(2, TestFunc())

However, when you're talking about a temporary local variable, as in your example, you normally just create the temporary variable then ignore it. This avoids the overhead of the call to select. Sometimes you use a variable name that indicates it's trash:

local _, var = TestFunc()

If say I was doing a pairs loop over 100 values, would that even have a signifigant impact?

None whatsoever. You're just continually overwriting the value of a local variable.

like image 67
Mud Avatar answered Nov 26 '22 16:11

Mud