-
Posted by Nosnam on Tue, 11 Nov 2008 01:14:51
When I typed up the test1 function on page 71, then tested it by passing it a nil value, it didn't have any problem printing it the arguments back out, including nil. Anyone have any insight?
Also, I'm wondering - the book notes that "the first function needs to create a new table on each call, which would allocate and use more memory in the long run" Since this is a local table, won't it be unset as soon as the function ends?
-
Posted by Nosnam on Tue, 11 Nov 2008 01:14:51
When I typed up the test1 function on page 71, then tested it by passing it a nil value, it didn't have any problem printing it the arguments back out, including nil. Anyone have any insight?
Also, I'm wondering - the book notes that "the first function needs to create a new table on each call, which would allocate and use more memory in the long run" Since this is a local table, won't it be unset as soon as the function ends?
-
Posted by jnwhiteh on Tue, 11 Nov 2008 05:06:17
When I typed up the test1 function on page 71, then tested it by passing it a nil value, it didn't have any problem printing it the arguments back out, including nil. Anyone have any insight?
The problem is specifically that it can work, but the semantics of Lua do not guarantee that it will work. As made very clear in the chapter on tables, you cannot rely on the length operator or most of the table functions if your table has a nil value in the sequence you expect to be counted as a single list.
Also, I'm wondering - the book notes that "the first function needs to create a new table on each call, which would allocate and use more memory in the long run" Since this is a local table, won't it be unset as soon as the function ends?
That's not really the way garbage collection works. The first function creates a new table each time and when the function ends, the table is eligible for garbage collection. This does not mean it is unset or deleted, it continues to take up memory until the table is fully collected. Depending on how long your garbage collection sweep cycle takes and how often you call the function this can be quite a bit.
The other advantages to using
...
andselect()
are much more important. You should never use tables for vararg functions, unless there is no other way to accomplish what you are trying to do.