lua comma operator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

lua comma operator

albertmcchan
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




Reply | Threaded
Open this post in threaded view
|

Re: lua comma operator

Soni "They/Them" L.


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.


Reply | Threaded
Open this post in threaded view
|

Re: lua comma operator

Egor Skriptunoff-2
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)
end

t.a, t.b = f("c"), f("d")

Output:
c
d
b
a


Reply | Threaded
Open this post in threaded view
|

Re: lua comma operator

Egor Skriptunoff-2


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()
end

t[f("a")], t[f("b")] = f("c"), f("d")

Output:
a
b
c
d
B
A

Reply | Threaded
Open this post in threaded view
|

Re: lua comma operator

albertmcchan
In reply to this post by Egor Skriptunoff-2
On Feb 11, 2018, at 11:01 AM, Egor Skriptunoff <[hidden email]> 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:

local t = setmetatable({}, {__newindex =
   function(t, k, v)
      print(k)
   end
})

local function f(v)
   print(v)
end

t.a, t.b = f("c"), f("d")

Output:
c
d
b
a


nice trivia, that means 

a, b, c, a = 3 same as a=nil; c=nil, b=nil; a=3

= a
3


Reply | Threaded
Open this post in threaded view
|

Re: lua comma operator

Tim Hill
In reply to this post by Egor Skriptunoff-2


On Feb 11, 2018, at 8:01 AM, Egor Skriptunoff <[hidden email]> 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:

local t = setmetatable({}, {__newindex =
   function(t, k, v)
      print(k)
   end
})

local function f(v)
   print(v)
end

t.a, t.b = f("c"), f("d")

Output:
c
d
b
a



Yes it’s easy to see this is current implementation behavior. But Lua does not _specify_ this, so if you rely on it in your code be prepared for your code to break on some Lua implementations.

It’s similar to this code in C:

Int i = 1;
I = i++ + ++I;

—Tim