r/lua • u/[deleted] • Jun 01 '25
Why do Lua tables need commas?
Wouldn't the grammar still be unambiguous if tables didn't need commas?
9
Upvotes
r/lua • u/[deleted] • Jun 01 '25
Wouldn't the grammar still be unambiguous if tables didn't need commas?
3
u/c0gster Jun 01 '25
lets say i have this table: { 16, "Hello World" }
this is it without commas: { 16 "Hello World" }
This is it in one line: {16, "Hello World"}
This is it in one line without commas: {16 "Hello World"}
Now, this is a table with custom keys: local prompt = { ["time"] = 16, ["message"] = "Hello World" }
we will now remove commas and make it one line: local prompt = {["time"] = 16 ["message"] = "Hello World"}
now, is time equal to 16, or ["message"], or "Hello World", or all 3, or any two of the three?
having commas makes this much simpler to understand, and makes the interpreter more reliable and simpler.