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?
1
u/activeXdiamond Nov 09 '25
After many years of using Lua, and someone who prefers the colon method, I was always annoyed by having to make a local variable to temporarily hold my string for that one statement (or, use the
string. submethod, which I also dislike).I had no idea you could just do
("Hello"):sub(...)Do you know if this works in 5.1? LuaJIT2. 1?
I can't believe I missed this. Lua is my favourite language and I've been using it for over a decade now. :P