|
On 28-Sep-06, at 5:53 PM, Glenn Maynard wrote:
On Thu, Sep 28, 2006 at 10:46:58PM +0000, David Given wrote:arg = "5" print (arg+0)Likewise, some compiler sugar to convert to treat '+a' as '0+a' would beeasy, not involve any additional opcodes, and would optimise away to nothing in most cases.echo 'for i = 1,10000000 do local a = 5*(i); end' | time luac -l - echo 'for i = 1,10000000 do local a = 5*(0+i); end' | time luac -l - Lua's compiler doesn't do much (any?) optimization.
It could only optimize that by recognizing that i was a number; otherwise, 0+i could trigger an arbitrary metamethod. (The same comment applies to - -i). Here I think that use of tonumber() is better, particularly since tonumber can be used as a test:
i = tonumber(s) if i then -- at this point i is known to be a number endIf I understood your desire for "unary +" correctly, your needs would be satisfied if the lexer recognized + as the first character in a numeric lexeme, no? The use case did not seem to require the use of + as an operator.