r/lua • • 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 • • Jun 01 '25
Wouldn't the grammar still be unambiguous if tables didn't need commas?
11
u/Zealousideal-Ship215 Jun 02 '25
Would ‘{3 -2}’ equal ‘{3, -2}’ or ‘{1}’ ?
5
u/fabricatedinterest Jun 01 '25
off the top of my head:
you can occasionally omit the parenthesis in a function call (when the argument is a single string literal or table literal), and function calls can return functions
so you can write for example "MyFunction{}{}{}{}"
so if the commas weren't in the syntax you couldn't write "{MyFunction {} {} {} {}}" intending to create a table containing MyFunction and four empty tables, because the language would read this as calling MyFunction and subsequent returned functions.
6
u/c0gster Jun 01 '25
because then how would you know when an item in a table has ended? sure, there are new lines, but there are also single line tables
3
u/Gnaxe Jun 01 '25
You might prefer https://fennel-lang.org/
2
u/didntplaymysummercar Jun 03 '25
No one mentioned but you can use semicolons instead of commas too, just a fun fact.
1
u/CirnoIzumi Jun 01 '25
its how you differentiate inline values in most grammars
1
u/SkyyySi Jun 01 '25
For starters, Lua completely ignores whitespace outside of using it as a word boundry (
return foovsreturnfoo) and in a very niche edge case with function calls followed by calling a parenthesized expressions (f() (function() end)()could mean "call the result off()with the argumentfunction() endand then call the result of that" or "callf, then callfunction() endwith no arguments" - Lua throws a syntax error here). Line breaks have no impact on terminating a statement or expression.In practice, this means that the table
is identical to
Since Lua allows calling a function with a single string (
f"..."==f("...")or table (f { ... }==f({ ... })) literal without parenthesis, that would actually translate tosome_variable("a string")!-7
u/bidaowallet Jun 01 '25
They are used as some form of OOP concept