[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: coercion to strings and numbers
- From: Rici Lake <lua@...>
- Date: Mon, 29 Jan 2007 11:51:50 -0500
On 29-Jan-07, at 11:11 AM, Doug Rogers wrote:
I'm curious about the efficiency gains. How often is this construct
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
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.