What does this syntax mean?
Note the lack of a comma between the first _ and the 2nd _, that's what I'm talking about.
"local _ _, x, y = ..."
Moderator: Moderator
-
- Sher'Tul Godslayer
- Posts: 2402
- Joined: Tue Jun 18, 2013 10:46 pm
- Location: Ambush!
Re: "local _ _, x, y = ..."
From playing around on repl.it, it looks like it's another way of saying
In other words, it's probably a bug, and it likely makes x and y global.
Some sample code demonstrating this:
Code: Select all
local _
_, x, y = ...
Some sample code demonstrating this:
Code: Select all
function ret4() return 1, 2, 3, 4 end
function test()
local _ _, x, y = ret4()
print(_)
print(x)
print(y)
end
test() -- prints 1, 2, 3; 4th return value is discarded
print(_) -- prints "nil"
print(x) -- prints "2"; i.e., x is global
print(y) -- prints "3"; i.e., y is global
Re: "local _ _, x, y = ..."
Yup, and it's not a bug 
I use it this way because x and y already exist

I use it this way because x and y already exist
[tome] joylove: You can't just release an expansion like one would release a Kraken XD
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning
--
[tome] phantomfrettchen: your ability not to tease anyone is simply stunning

Re: "local _ _, x, y = ..."
Ah, I see. So, Doctornull, to answer your question, the intent is something like "declare _ as a local variable, then assign to _ and the preexisting local variables x and y" (or, really, "throw away the first return value by storing it to new local variable _, then store the second and third return values to preexisting local variables x and y").darkgod wrote:Yup, and it's not a bug
I use it this way because x and y already exist
Thanks for the explanation and correction.
-
- Sher'Tul Godslayer
- Posts: 2402
- Joined: Tue Jun 18, 2013 10:46 pm
- Location: Ambush!
Re: "local _ _, x, y = ..."
Interesting, thanks for the explanation.
I'm a bit curious why it's desirable to have global x and y but not a global "garbage" variable _.
I'm a bit curious why it's desirable to have global x and y but not a global "garbage" variable _.
Re: "local _ _, x, y = ..."
x and y aren't globals; they're locals that are declared earlier in the function (so there's no need to declare them again).Doctornull wrote:I'm a bit curious why it's desirable to have global x and y but not a global "garbage" variable _.
Global variables are almost never desirable; using globals means that functions' behavior can affect and be affected by arbitrary global variables in addition to the functions' own inputs and outputs, and that simply makes code too hard to understand and maintain.
-
- Sher'Tul Godslayer
- Posts: 2402
- Joined: Tue Jun 18, 2013 10:46 pm
- Location: Ambush!
Re: "local _ _, x, y = ..."
Ah, yes, I'd missed that. Thanks!Castler wrote:x and y aren't globals; they're locals that are declared earlier
Indeed, thus my question.Castler wrote:Global variables are almost never desirable
Cheers!