"local _ _, x, y = ..."

All development conversation and discussion takes place here

Moderator: Moderator

Post Reply
Message
Author
Doctornull
Sher'Tul Godslayer
Posts: 2402
Joined: Tue Jun 18, 2013 10:46 pm
Location: Ambush!

"local _ _, x, y = ..."

#1 Post by Doctornull »

What does this syntax mean?

Note the lack of a comma between the first _ and the 2nd _, that's what I'm talking about.
Check out my addons: Nullpack (classes), Null Tweaks (items & talents), and New Gems fork.

Castler
Thalore
Posts: 153
Joined: Mon Mar 25, 2013 10:09 pm

Re: "local _ _, x, y = ..."

#2 Post by Castler »

From playing around on repl.it, it looks like it's another way of saying

Code: Select all

local _
_, x, y = ...
In other words, it's probably a bug, and it likely makes x and y global.

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
Qi Daozei (QDZ) - an Oriental-themed fantasy game for T-Engine. ToME Tips - auto-generated spoilers for ToME.

darkgod
Master of Eyal
Posts: 10750
Joined: Wed Jul 24, 2002 9:26 pm
Location: Angolwen
Contact:

Re: "local _ _, x, y = ..."

#3 Post by darkgod »

Yup, and it's not a bug :)
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 ;)

Castler
Thalore
Posts: 153
Joined: Mon Mar 25, 2013 10:09 pm

Re: "local _ _, x, y = ..."

#4 Post by Castler »

darkgod wrote:Yup, and it's not a bug :)
I use it this way because x and y already exist
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").

Thanks for the explanation and correction.
Qi Daozei (QDZ) - an Oriental-themed fantasy game for T-Engine. ToME Tips - auto-generated spoilers for ToME.

Doctornull
Sher'Tul Godslayer
Posts: 2402
Joined: Tue Jun 18, 2013 10:46 pm
Location: Ambush!

Re: "local _ _, x, y = ..."

#5 Post by Doctornull »

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 _.
Check out my addons: Nullpack (classes), Null Tweaks (items & talents), and New Gems fork.

Castler
Thalore
Posts: 153
Joined: Mon Mar 25, 2013 10:09 pm

Re: "local _ _, x, y = ..."

#6 Post by Castler »

Doctornull wrote:I'm a bit curious why it's desirable to have global x and y but not a global "garbage" variable _.
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).

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.
Qi Daozei (QDZ) - an Oriental-themed fantasy game for T-Engine. ToME Tips - auto-generated spoilers for ToME.

Doctornull
Sher'Tul Godslayer
Posts: 2402
Joined: Tue Jun 18, 2013 10:46 pm
Location: Ambush!

Re: "local _ _, x, y = ..."

#7 Post by Doctornull »

Castler wrote:x and y aren't globals; they're locals that are declared earlier
Ah, yes, I'd missed that. Thanks!
Castler wrote:Global variables are almost never desirable
Indeed, thus my question.

Cheers!
Check out my addons: Nullpack (classes), Null Tweaks (items & talents), and New Gems fork.

Post Reply