r/lua • u/RiverBard • Nov 09 '25
Discussion Syntax conventions, string.foo(bar) vs bar:foo()
I'm primarily a Python programmer but I've been learning Lua for various reasons and I am coming to appreciate its sleek nature the more I use it.
That said, I'm grappling with the different syntax options that Lua provides. In the Pythonic mantra, the line There should be one-- and preferably only one --obvious way to do it. comes to mind; while it is nice to have options I feel like I should choose one way or another and stick to it. Is this how Lua programmers typically operate?
If I were to stick with one, which to do? Again referencing the Pythonic way, we have Explicit is better than implicit. and Sparse is better than dense. and Readability counts., which to me would point to using string.foo(bar) instead of the : syntactic sugar, but it also isn't quite as compact.
What are your thoughts? Am I just overthinking things and applying Pythonic logic where I shouldn't? How do Lua programmers typically write their code?
4
u/HelioDex Nov 10 '25
For strings specifically, I always use
string.foo(bar), even though I thinkbar:foo()is more common.This is because I mainly write Luau, in which the
string.foo(bar)syntax is implemented using the normalGETIMPORTandCALLbytecode operations rather than aNAMECALL(on -O1 and up). It's also (very slightly) more performant. I think the performance is near identical in most other Lua implementations though.The functions on the
stringlibrary feel more in-line with the rest of Lua's standard library to me, so I don't mind the increased verbosity.2
u/Previous-Traffic-130 Nov 09 '25
i prefer using ":" its way cleaner
2
u/Ed_Blue Nov 09 '25
I treat it as what it is: Syntax sugar (for self). I typically use it universially only when doing OOP.
2
u/smog_alado Nov 10 '25
Don't worry to much about the "Zen"; Python itself has has many instances where it offers more than one way to do the same thing.
2
u/appgurueu Nov 09 '25
I prefer
:. It's more concise and chains more nicely, e.g. you can write something likes:gsub("%s", ""):sub(42).string.sub(string.gsub(s, "%s", ""), 42)is more awkward to read and write.There are some edge cases, like
string.format, where"":format(...)is not valid so you need to use(""):format(...)instead, orstring.char, where the first argument is a number not a string, so you can't do(42):char()and need to dostring.char(42)instead.There are also some subtle differences in that
string.foo(x)will coercexto a string if it is a number, whereasx:foo()will throw an error. On the other hand, ifxis a string-like object that implementsfoovia its metatable, things can work just fine. (I would consider this another advantage of the:methodsyntax: It lets you use dynamic dispatch should you need it in the future.)1
u/weregod Nov 12 '25
In PUC Lua self call is not syntax sugar, foo:bar() use less opcodes then foo.bar(foo).
With optimizing compilers (LuaJIT, Luau) this may be less relevant but I prefer optimal syntax.
But for strings storing functions in local variables usually will be faster because strings use metatable.
So I use foo:bar() for tables but
0
u/[deleted] Nov 09 '25
[deleted] — view deleted comment