# lua comma operator

6 messages
Open this post in threaded view
|

## lua comma operator

 does comma operator always process from left to right ? is it in the manual ? items = 0 function count(...)     items = items + select(''#', ...)     return items end = count(1,2,3), items 3     3 = count(1,2,3), items 6     6 = items, count(1,2,3) 6     9 = items, count(1,2,3) 9     12
Open this post in threaded view
|

## Re: lua comma operator

 On 2018-02-11 11:55 AM, albertmcchan wrote: > does comma operator always process from left to right ? is it in the manual ? > > items = 0 > function count(...) >      items = items + select(''#', ...) >      return items > end > > = count(1,2,3), items > 3     3 > = count(1,2,3), items > 6     6 > > = items, count(1,2,3) > 6     9 > = items, count(1,2,3) > 9     12 > > > > I don't think Lua specifies argument evaluation order, either. I can't find anything in the manual. Lua makes no guarantees about the execution of expressions, only statements. (Aka x(y(), z()) is UB) The only guarantee it seems to make is "Both `and` and `or` use short-circuit evaluation; that is, the second operand is evaluated only if necessary.": http://www.lua.org/manual/5.3/manual.html#3.4.5-- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: lua comma operator

 In reply to this post by albertmcchan On Sun, Feb 11, 2018 at 4:55 PM, albertmcchan wrote:does comma operator always process from left to right ?It's easy to check that both vanilla Lua and LuaJIT prepare values from left to right and then assign them from right to left:local t = setmetatable({}, {__newindex =   function(t, k, v)      print(k)   end})local function f(v)   print(v)endt.a, t.b = f("c"), f("d")Output:cdba
Open this post in threaded view
|

## Re: lua comma operator

 On Sun, Feb 11, 2018 at 7:01 PM, Egor Skriptunoff wrote:On Sun, Feb 11, 2018 at 4:55 PM, albertmcchan wrote:does comma operator always process from left to right ?It's easy to check that both vanilla Lua and LuaJIT prepare values from left to right and then assign them from right to left:More interesting example:local t = setmetatable({}, {__newindex =   function(t, k, v)      print(k)   end})local function f(s)   print(s)   return s:upper()endt[f("a")], t[f("b")] = f("c"), f("d")Output:abcdBA