lua-users home
lua-l archive

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



On 29-Jan-07, at 11:11 AM, Doug Rogers wrote:

I'm curious about the efficiency gains. How often is this construct used
in time-critical code? I can understand that we would want
table.concat() to use tricks for efficiency gains, but I'm not so
convinced about violating the universality of metamethod __concat for a
.. b .. c. And my own sensibilities do not like the 'or type(opX) ==
"number"' part of the rule.

I use it a lot. Consider the not-too-uncommon case:

  'a little prefix ' .. really_long_string .. ' a litle suffix'

If .. doesn't coalesce, you'll end up unnecessarily making a temporary (really_long_string .. 'a little prefix') and then immediately throwing it away. Python programmers routinely use "%s%s%s" % (a, b, c) to avoid this problem, but Lua is a lot more readable without sacrificing efficiency.

It doesn't violate the universality of __concat any more than the fact that you can't override __add for numbers, in my opinion. The optimization only applies to strings, and since string concatenation is associative, there is no semantic difference.

From time to time, I've wished that I could override the internal __concat with multiple parameters (for example, to concatenate a number of arrays) but the semantics of that might be a bit odd.