|
what's the difference? I remember in pairs is a banned practice but I don't remember why |
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
They're essentially no different, however, I recommend you use "for _,v in next,table do"
pairs() calls upon next, from what I remember it's just an extra seemingly unnecessary step but if you want your table in parenthesis for organization's sake, then go for it.
|
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
There is no functional or performance difference. You remembered wrong.
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
Who said it was a banned practice?
Was it Time? ;P
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
Never said there was a functional difference but I explicitly remember being told that in pairs() just returns the next function
-- from the lua site
function pairs (t)
return next, t, nil
end
so you might as well use next, and save yourself the keystrokes for the parenthesis()
|
|
chimmihcJoin Date: 2014-09-01 Post Count: 17143 |
My post wasn't a reply to yours. Look at the times.
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
Oh! Hah :)
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
A question, though: how is there no performance difference?
If it's comparable to this, then...
function Bar()
return "somedata"
end
function Foo()
return Bar()
end
data Bar() would be (albeit unnoticeably) faster than Foo(), no?
|
|
|
I don't remember incorrectly ;^)
https://forum.roblox.com/Forum/ShowPost.aspx?PostID=192642752
"The pairs function makes a new version of next every time you call it. It's bad. Using pairs should be an outlawed practice unless readability demands it."
I don't really get what that means though.
|
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
She's just trying to say the extra step is unnecessary "unless readability demands it" which I said almost verbatim.
If you want parenthesis cuz they help you organize, do it up.
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
She means what I told her on slack. Every time you call pairs, Lua retrns a new closure of next. It's not bad or anything, it's just how it works. Same when you get a CFrame/Vector3 method.
print(cf.toWorldSpace, cf.toWorldSpace)
Pairs only costs ONE extra function call BEFORE everything happens, meaning the size of your array doesn't affect the negligible speed difference because the next function is still being used for iterating. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Actually not sure if I told her on slack or if it was someone else whatever. |
|
SoybeenJoin Date: 2010-02-17 Post Count: 21462 |
So why does pairs even exist?
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Consistency (because ipairs exists) and readability (some people prefer calling pairs because it might be "prettier" to them). |
|
|
Thank you #### ##### #### That's been bothering me for close to half a year now. |
|