r/lua • u/st3f-ping • Apr 08 '25
Discussion Comma separated assignment joy
Just wanted to share something simple that gave me joy.
I've used multiple assignments before but only in a very limited way. If I have two values I need to return from a function I'll return them as comma separated values so I'll assign them like this:
x, y = somefunction(somevalue)
And that has been the extent of how I have used them.
But yesterday I had a programming problem where two interdependent variables needed to be updated atomically. Something akin to (but more complicated than):
--to be done atomically
x = x+y
y = x-y
Now I obviously can't write it like that as by the time I get to the second assignment, x is no longer the value it needs to be.
I could write something like...
local oldx=x
x = x+y
y = oldx-y
...but that's really ugly. I could hide the ugly in a function...
x, y = update(x,y)
...but, on a chance I decided to try a comma separated expression to see if they were atomic...
x, y = x+y, x-y
...and it worked. The second half of the expression is evaluated without considering the update to the values made by the first half. It works as calculate, calculate, assign, assign. It made me so happy.
Now this may seem like bread and butter coding to some of you. And some of you may look down on me for not knowing all of the nuance of the language I am using (and probably rightly so) but this made me really happy and I thought I'd share in case any of you might enjoy seeing this.
(edit: typos, tidying for clarity, and some auto-uncorrect of code samples)
(edit2: manual page for assignment is here where it states that "In a multiple assignment, Lua first evaluates all values and only then executes the assignments." :) It even gives the example of swapping two values x, y = y, x. I must have read that page half a dozen times over the years. Glad it has finally sunk in.)
5
u/Motor_Let_6190 Apr 08 '25
Lua is like that for a few or many of us : makes you want to code for the sake of coding. Heck, I forked LuaJIT yesterday as in parallel to hobby/Indie gamedev, I want to hack around VMs, language development, JIT trace compiling just for the sake of it. Have fun, cheers !
3
u/vitiral Apr 08 '25
It's even better: the behavior you noticed isn't some kind of quirk, it's an obvious behavior from how the Lua stack works. Variable assignment is done from the stack result of the expression, and obviously the stack must be created before the variables can be assigned.
Because Lua functions operate on a stack (at the C level as well) all sorts of simplicity like this bubble up
2
u/paulstelian97 Apr 08 '25
Lua has some funny pattern matching when it comes to comma assignment
Maybe if you do something like x, y, z = 5, 2 you will see nil in z
11
u/Cultural_Two_4964 Apr 08 '25
Cool post.
3
u/benjamarchi Apr 08 '25
Lua is awesome
2
u/AutoModerator Apr 08 '25
Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/False-Tourist9825 Apr 10 '25
It improves performance (at least in luau). I tested it with these two:
What happens is that 'local' takes multiple "arguments" (not sure how it is called) and it improves performance but compromises readability if you are new to it.
1
u/lambda_abstraction Apr 18 '25
Just like psetq. ;-)