lua-users home
lua-l archive

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


Hi,

Romulo Bahiense wrote:
> Note2: Most prefer to name the locals as: "local _,_,path,file = ..."
> Note3: In the not-yet-released Lua 5.1, you could use the built-in 
> function "select" to skip those unnecessary values: "local path,file = 
> select(3,string.find(....)))"

You could -- but you shouldn't. It's a lot slower to call a
C function (frame setup/teardown overhead) and to copy the
results around several times than to just leave those two
variables dead. Stack slots are abundant and can be reused
in the same function if you care (wrap a block in do ... end).

It's also cheaper to use new local variables each time than
to reuse them. The bytecode compiler just overlaps them with
the call results in the first case, but needs to copy them
around in the second case.

I often use sequences like this:

  local _,_,a,b = string.find(...)
  if a then ... end
  local _,_,c = string.find(...)
  if c then ... end

Don't be ashamed of those '_' variables. It's a Lua idiom,
no more, no less. It makes sense, too.


The 'select' function is really only intended for vararg
expressions, like in 'select(3, ...)'. Because you can't name
the individual arguments of it. However this is fairly uncommon
because you can mix fixarg and vararg arguments in the function
declaration. There is no point in introducing a new syntax just
for this case, so that's how select() was born.

Bye,
     Mike