Are tuples the same thing as tables? If they are different what is a tuple and how is it different from a table. And also what is unpack and pack. Uh also can you not use advanced words to answer I can’t really understand advanced words. Any help would be appreciated.
Tuples are basically multiple values returned by some functions.
for example this custom function returns a tuple
function getplrdata(player)
return player.Name, player.UserId
end
local username, userid = getplrdata(game.Players.LocalPlayer) -- returns a tuple, ddivided by commas
print (playerid, userid)
Some functions also have this. For example pcall() returning if the script ran without errors, and the error string if there was an error.
FindPartOnRay() also has this.
So is it similar to a table? And what is the difference
No, it is not at all, but of course you can always put it into a table with table.insert
These are basically simple variables.
It’s kinda like a table but not really. There are no keys or anything, it’s more like an array where you don’t have control of a key.
For example, a table would be like
function foo()
return {foo="bar", test=true}
end
local x = foo()
print(x.foo) --> "bar"
whereas a tuple would be like tthis:
function foo()
return "bar", true
end
local x, y = foo()
print(x) --> "bar"
So its basically an list of values like maybe like cordinates?
A tuple is an ordered list of elements / variables. Some functions and events can accept and / or return a tuple as an argument / parameter, such as:
[FindPartOnRay]:
FindPartOnRay returns a tuple. In order and when there's an intersection, it returns: the part it hit, the position of the intersection, the surface normal of the part, and the material it hit:local part, position, normal, material = workspace:FindPartOnRay(Ray.new(Vector3.new(), Vector3.new(0, 0, -1) * 5))
print(part, position, normal, material)
as well as, remote events and functions.
Tables and tuples are kinda the same, being that they’re both an ordered list of elements, but they are more different.
unpack
returns a tuple of the table’s elements:
local a, b, c = unpack({1, 2, 3})
print(a, b, c)
If you were to just do: print(unpack {1, 2, 3} )
, it’d print all of it’s elements, as print is another function that accepts tuples are parameters.
pack is the opposite
Not necessarily coordinates, but if you’d like to think of it like that then yes.
It’s an arbitrary list of values that get returned to you by a function.
Lua Tables
Lua tables are really tricky, since in Lua they’re extremely versatile. In many other programming languages the concept of a construct like a Lua table doesn’t really exist; its functionality is split up across many different types of constructs. (simplified)
Tables as dictionary-like objects
Tables can have properties that are computed, consider the following:
-- Here is a dictionary-like table. Values are stored in key-value pairs where
-- each key is computed. By default, they are implicitly string keys.
local dictionaryOne = {
one = 1,
two = 2,
three = 3,
};
-- Here is a dictionary-like table initialized in a different way. It is no
-- different than "dictionaryOne" at runtime.
local dictionaryTwo = {};
dictionaryTwo.one = 1;
dictionaryTwo.two = 2;
dictionaryTwo.three = 3;
-- Here is yet another dictionary-like table where every key is explicitly
-- computed. It is no different than "dictionaryOne" or "dictionaryTwo" at
-- runtime.
local dictionaryThree = {
["one"] = 1,
["two"] = 2,
["three"] = 3,
};
Tables as array-like objects
Tables have an alternative form: arrays. Consider the following:
-- Here is an array-like table. It only has integer keys.
local arrayOne = {"one", "two", "three"};
-- Here is an array-like table with its integer keys explicitly written. It is
-- no different than "arrayOne" at runtime.
local arrayTwo = {
[1] = "one",
[2] = "two",
[3] = "three",
};
So, what are tuples?
Tuples are just array-like tables with a finite known number of indexes (which are often immutable its values can not be changed)
Tuples are not natively supported by Lua as they should be thought of as immuatable. This is not enforced at runtime.
This is a tuple of length three:
-- An array-like table with a length of three.
local tuple = {1, 2, 3};
The following is not a tuple; it is an extremely simplified version that Roblox has stated is idiomatic of its documentation but not of Lua.
Tuples are not defined this way in Lua, any other programming language, or mathematics. For the type theory behind it, refer to here: Tuple - Wikipedia
-- Roblox reference says this is a tuple. It's *really* not.
local elementOne, elementTwo, elementThree = unpack({1, 2, 3});
The takeaway:
When reading the documentation for Roblox, know that tuples are just “a list of elements”. When formally discussing tuples to other developers, (even in the realm of Lua), don’t make this mistake. (Lua’s formal documentation doesn’t even use the word “tuple” since it knows its not native. It simply states functions return x, y, z
values.)
Note:
“Tables can have both integer keys and computed keys! Why didn’t you include that!”
Don’t do this.
Seriously, don’t.
Unless if you really know what you’re doing and you’re implementing an enumeration of sorts with reverse-mapping, you seriously should not do this.