lua-users home
lua-l archive

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

On 17-Oct-05, at 5:06 PM, Glenn Maynard wrote:

I guess this case is impossible to detect at compile-time, though. That
seems to be a common problem with Lua; I suppose any warning facility
would need to be runtime.  Yuck.

I'm getting quite used to the run-time "warning"; it looks like this:

foo:27: attempt to call a table value

Now, Lua has no compile-time warning mechanism, and it is not particularly clear how one would even add one to the compiler (possibly an optional callback function to accept warnings?), but in principle it is easy enough to detect most instances of the "missing-pairs" problem at compile-time: you simply check for the case in "for <varlist> in <exprlist> do" where <exprlist> is a single expression which is not a function call of some kind. That's an imperfect check, but it would be good enough for a warning.

This is somewhat like the famous C error:

  while (ch = ' ') { ... }

Many C compilers will warn you about that, if asked to, suggesting that you write it as:

  while ((ch = ' ')) { ... }

if that's truly what you meant (unlikely, in this case); similarly, you could quell the suggested Lua warning (that is, the suggestion I made a couple of paragraphs ago) by writing:

  for val in func, nil do

instead of

  for val in func do

if that's what you meant (as opposed to "for val in pairs(func) do")

I'm not sure why Lua deserves any more Yucking than C in this pair of vaguely comparable cases, although one would certainly prefer Lua to be less Yuckworthy.