lua-users home
lua-l archive

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



On 28/09/2006, at 9:43 PM, David Jones wrote:

I think you could argue that one should have unary plus on grounds of symmetry. :)


I agree with this, it seems strange you can do:

a = -5

but not:

a = +5

I don't think it would be a no-op, it should coerce its operand to a number, just like unary minus does:

That would save a bit of space, rather than writing tonumber (arg) when you really want a number.

Thanks to this tip, you can coerce strings into numbers right now, rather clumsily:

arg = "5"
print (- -arg) --> 5

One could avoid unary plus adding to the VM by compiling +x as (- - x) which would in most cases get constant folded anyway.

At present, the two consecutive UNM do not seem to be eliminated:

a = - -"5"


        1       [1]     LOADK           0 -2    ; "5"
        2       [1]     UNM             0 0
        3       [1]     UNM             0 0
        4       [1]     SETGLOBAL       0 -1    ; a
        5       [1]     RETURN          0 1

- Nick Gammon