lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Veli-Pekka Tätilä wrote:
Does that mean that a string is needlessly copied by value when I pass it to a function (ala Perl if you copy args to my variables in that language)?

You could describe strings in Lua as passed by reference to an immutable array of char.
And just to clarify, parameter passing is no different to copying locals.

Does it make sense to use the terms array, record, hash and sparce array depending on whether you'll use the table for storing elems by int indeces, named fields, looking up stuff by arbitrary data or storing noncontiguous runs of int indexed elements?

The manual and lua list tend to call tables indexed by numbers from 1..n as arrays, and other kinds of tables tables.
I haven't seen record or struct used anywhere.
Anyway, I'd suggest not attempting to name every conceivable use of tables - describe it based on what it's doing at the time. Eg a binary tree implementation in Lua would likely call its tables nodes, a matrix implementation will probably call its tables rows, etc.

3. There's a bit in the ref man about coroutines I'd like clarified:

Quote about yield:
Suspends the execution of the calling coroutine. The coroutine cannot be running a C function, a metamethod, or an iterator.
End quote.

Umm what's this about?

Unfortunately it's just as it sounds. It's a limitation imposed to avoid making the implementation a lot more complicated, and may not be present in future versions of Lua. LuaJit supports yields anywhere, using non-portable code, but unfortunately makes coroutines a lot larger as a result (each is given a dedicated C stack). LuaJit 2.x avoids this, but will still support yielding anywhere. Don't misunderstand though - you can yield from within a for loop. Just in this example:
 for k,v in mypairs(tab) do end
the function mypairs (well, the iterator returned by it) cannot yield.

To see why the limitation is in place, consider a simple gettable. The vm calls luaV_gettable, which looks up the reference. If it finds nil, it calls the metamethod (if present). If this metamethod were to yield, somehow the vm needs to remember that this thread was half way through a gettable operation, and needs to handle the "results" passed to the corresponding resume accordingly. It then would need to abnormally exit the gettable function, and run the next coroutine in the vm, and upon resuming somehow end up back in the correct spot in the luaV_gettable. There are quite a few neat ways to handle it, and some things, ie making iterators yieldable, are simpler to solve then others (http://lua-users.org/wiki/YieldableForLoops), but don't hold your breath.

- Alex