lua-users home
lua-l archive

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


For this note, assume that a word surrounded by the underline
character means the word should be typeset in italics.

Roberto Ierusalimschy <roberto@inf.puc-rio.br> writes:

> Again, it is easy for the interpreter to test whether the called function 
> is C or Lua, and act accordingly. The real problems I see for tail calls in 
> Lua are
> 
> 1 - debugging. Many Lua users do not even know what is a tail call, and
> expect to see them in a stack trace.
> 
> 2 - subtle behaviour. "return f()" is a proper tail call (*if* f is a Lua
> function), but "return x, f()" and "f(); return" are not. This can be
> quite confusing for programmers assuming proper tail calls.

The subtle behavior you point out suggests to me a simple rule for the
identification of tail calls.  The rule is easy for programmers to
understand.  The rule could be:

  A call is a tail call if and only if it occurs in a statement of the
  form "return _tailcall_", _tailcall_ is a Lua function call
  expression _functioncall_, and the function part of the function
  call evaluates to a Lua function.  The syntactic category
  _functioncall_ is defined in the section of the manual titled "The
  complete Syntax of Lua".  

In other words, only statements of the form: 

  return _varorfunct_ [ ':' _name_ ] _args_

are tail calls, and only when

  _varorfunct_ [ ':' _name_ ]

evaluates to a Lua function.

John