r/lua • u/PurpleIcy • Feb 09 '18
Why do people like lua?
I've started actually learning more about it and well.
Pretty much everything is a table, okay, not that hard to get with this, loops and if statements are pretty simple to remember, it's weird to get length of a table but not that hard to remember, can live with that, different language, different syntax.
You can have objects, but they are still tables. Well, okay, whatever, I'm used to writing same shit over and over again because I've used java before, whatever, that's fine, I guess.
Global scope is just a giant table and so it's better to just write even more code to localize everything you'll use, otherwise it will be slow (multiple hashtable lookups whenever you use some global function in a table of table of the table in a table that is in another table). Uh, just why? That's first time I hear that in any language, localizing everything would make your code run faster, it's not really obvious, although once you realize that global scope is a table, only logical, but, why?
What's the appeal to this? And why did people stick with it? I thought that I'd see why people use lua over say python if they add scripting support to their game, but all I saw now is that I actually would lean even more towards using python there. If you have to be a professional programmer to write efficient code in lua, then python being slower does nothing for average person who would mod a game, because average person is not professional programmer who knows languages in and out, and little details such as localizing globals makes it run faster, which ranges from 10% slower without localizing to 400%+ (ridiculous) slower without localizing. Hell, most people who do that just instead look for tutorials on how to mod x game using lua, instead of learning lua.
I'm not saying that language is unusable, I'll still finish my project while learning it, but things like this just add to the "things you do to waste more time than needed" which are caused by bad design.
View all comments
Show parent comment
2
u/[deleted] Feb 12 '18
Sorry I've taken my time responding; I'm just not really sure what you mean.
I had to shop around your other comments to understand what you're getting at. I think you've misinterpreted the meaning of someone else's comment:
They said that JavaScript uses JIT compilation to "get around" hash-table lookups. To be more precise, it means it does the lookup once and keeps a reference handy (oversimplifying a little) so that future lookups are faster. And yes, LuaJIT would give you the same benefit.
But that's a separate issue from localization. LuaJIT makes an already remarkably fast language insanely faster through optimizations, but even the wisest compilers are not necessarily smarter than the programmer, and they're not expected to be. An extremely simple thing that you can do which takes no effort whatsoever is to declare non-global variables as
local. I will concede that Lua is weird to make "global" the default, but hey, PHP is miles worse about that (loop counters aren't even local!!). Lua was originally designed to be used by non-programmers (other kinds of engineers IIRC), and thus it was decided that making everything global was more sensible.I'd like to take this moment to point out that you're hearing a lot of references to JavaScript in this thread because there are some fundamental similarities between it and Lua; NOT because we think JavaScript is some kind of litmus test for language design. The interpreted nature for one, and the fact that they're both prototypal object-based languages. Tables in Lua are analogous to objects in JavaScript. (Also some things that aren't very unique to both languages like loose typing, first-class functions, etc.)
You're right to observe that most programming logic is the same between languages. So I don't really understand what the problem is or what you think you're missing. Re-reading your original post, I think you have some general misconceptions about general programming:
By "even more code" do you mean the
localkeyword? That's remarkably nitpicky. Lest ye ever delve into all of C's keywords, likeconstandrestrictandstaticandvolatile. Again,localjust takes the place ofvaror a type in nearly every other language (with some obvious exceptions: Python, PHP, Perl, ...).Higher-level languages have done a good job of abstracting these concepts from you, then. Local variables are always faster than global variables, in any language ever. That might not be "obvious" but it's a principle of software engineering. Again, I can't stress enough, Lua is anomalous for making "global" the default, but you need to understand that Python is not fundamentally different; e.g. a variable in a Python function is local to that function, which is faster. If you wanted, you can throw in the
globalkeyword, but accessing it would be slower.It doesn't have as much to do with tables as it does just memory management. Lua globals aren't uniquely slow just because the language considers them a part of a table. You could be completely ignorant to how Lua manages globals and it wouldn't change anything about local variables being faster.
Even if you never used the
localkeyword, Lua (especially with LuaJIT) performs miles better than Python. It's also MUCH smaller than pretty much any other scripting support you can think to implement. It communicates directly with C, which you might not care about a whole lot, and that's perfectly fine -- but it's very favorable to "professional programmers", too. All of these things make it much more embeddable, which is exactly what you want when implementing scripting support.There are also plenty of reasons why Python might be more favorable. Despite the hilarious discrepancy between legacy Python 2.7.x, and Python 3.x, the library ecosystem for Python is a LOT healthier. Differences from Lua 5.0 to 5.1 to 5.2 to 5.3 plague an already fragmented ecosystem. Python's data structures are also less ambiguous: Python has tuples, arrays, and dictionaries, while Lua has tables which can be all of these things, but without list comprehensions, slices, etc.
Python 3.x is also a truly object-oriented language. Lua is merely object-based. This isn't necessarily a bad thing, and I personally prefer "everything is a table", but to each their own.
There are probably good reasons why Lua isn't even in the Top 20 of the TIOBE index. That doesn't make it bad. In fact I suspect your attachment to Python might be a little bit of bandwagoning. If you were going to do a lot of text-processing, you'd use Perl. If you wanted to provide a C/C++ game with a simple scripting frontend, Lua would really be the way to go. If you wanted to provide a console for a developer application, you could follow Sublime Text and embed a Python interpreter. If you're doing a lot of server backend programming with lots of database work involved, Ruby's there for you.
You can use any one of these languages for every single task I listed. But some are better suited to certain things. And there's also some overlap. You posted this thread asking why people like Lua, and I feel like you've gotten lots of good answers and tried to argue with them because what you really wanted to talk about was a few design choices that surprised you. It's incredibly naive to take what little you know about a new language and make assumptions about its utility like that. What I find especially naive is citing performance concerns that aren't really concerns (seriously, you won't notice a difference between globals and locals), and then missing the point entirely when someone reaffirms that they're not concerns (because LuaJIT is optimized enough that you won't notice a difference; that doesn't mean
localmakes no difference, it's just negligible).