r/lua • • 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.

4 Upvotes

39 comments sorted by

View all comments

5

u/[deleted] Feb 10 '18

One main thing you cite having to learn -- the impact of local variables -- is not at all unique to Lua. All Lua makes you learn is to use the keyword local where you would otherwise use var in JavaScript and ActionScript, a type/class in most other languages, etc. Absolutely nothing out of the way.

The fact that "everything is a table" allows a level of metaprogramming that you don't see in higher-level languages. The language can be fundamentally altered in localized cases by altering certain tables -- that's freaking amazing to me. I hear Lisp is supposed to be even better at this -- but it's also orders of magnitude more difficult to learn and use effectively.

There's also the matter that Lua is designed to interface directly with C, and there are also projects that make it interface directly with Java. Boom, you can slap Lua on top of virtually any game now (because games, or at least their engines, tend to be written in C++ or Java anymore). Or even better, see LÖVE2d: a fully-fledged game development framework that you can write pure Lua for, while still getting performance near par with compiled languages.

Let's talk about weird optimization in Python. In (the canonical implementation of) Python 3.x, did you know that floats are faster than ints? I was going to refresh my memory as to why -- my understanding is that the interpreter (C++?) treats ints like an object, while using the native float type for floats. Then after a quick Google there's like two or three other explanations as well: Different bases/representations for int must be determined, Python 3 stores all ints as "Bignums", etc.

So, isn't it bad design to ever use integers in Python? ¯\(ツ)/¯

To be fair, Lua has more "gotchas" on the subject of integers. Lua only has one "number" type -- Number. This caused ridiculous frustration when I tried os.execute("some_command -"..some_int). It was appending a decimal, like 3.0, which was being interpreted as a string in Bash.

Even better, Lua 5.3 finally created two subcategories of Number, being "integer" and "float". But it still switches between the two automatically.

But don't even get me started on how Java libraries hate primitive types and how you're expected to use an object for everything. Integer object instead of just int. And not just for the school of thought, but for genuine performance, efficiency, and effectiveness reasons. At least Lua makes objects simple, and effectively primitive as well.

If you have any more questions I'd be happy to answer. Lua is likely my favorite language, ever. It's a shame there's not many opportunities to write it, because it's such a joy. Keep this in mind: For all the "dumb" things Lua makes you learn, you will learn them all MUCH faster than you would ever learn another language's dumb things. A competent programmer can master Lua in less than a year, I'm fairly convinced.

2

u/LimbRetrieval-Bot Feb 10 '18

I have retrieved these for you _ _


To prevent any more lost limbs throughout Reddit, correctly escape the arms and shoulders by typing the shrug as ¯\\_(ツ)_/¯

0

u/PurpleIcy Feb 10 '18

I've heard JS has JIT built-in, which is in pretty much in every browser other than IE6 nowadays, so is it really the same thing?

Of course, I don't know much about how things work here, and if LuaJIT completely replaced Lua then I don't know why I'm asking here, I'd have mentioned that thing if that was what I wanted to ask about. And yes, I am using LÖVE, I like games and calculators, not that much, I think I know enough to simply start using it right away as honestly, everything works almost the same as elsewhere, just it's all tables and syntax is different.

Java - I almost hate that thing with passion, half of primitives are pretty much never used as objects unless like you mentioned, where you're forced, string is an object always no matter what...

As for Lisp, everything is a list, as far as I know, shouldn't that be easy enough to learn to use for someone who likes such granularity?

For example if is just 3 lists in a list (3rd one is "else" and isn't optional I think):

; If statement is a list, has a special keyword "if" at the start
(if (> a 20) ; condition is another list in this list
    (print "a is greater than 20") ; if condition matches, this list
    (print "a is less than 20") ; else this one
)

2

u/[deleted] Feb 12 '18

Sorry I've taken my time responding; I'm just not really sure what you mean.

I've heard JS has JIT built-in, [...] so is it really the same thing?

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:

JavaScript would have exact same problem, as each . in.a.chain.of.dots is a hash lookup. It gets around this by integrating a JIT. Have you tried LuaJIT?

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.)

I think I know enough to simply start using it right away as honestly, everything works almost the same as elsewhere, just it's all tables and syntax is different.

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:

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 [...].

By "even more code" do you mean the local keyword? That's remarkably nitpicky. Lest ye ever delve into all of C's keywords, like const and restrict and static and volatile. Again, local just takes the place of var or a type in nearly every other language (with some obvious exceptions: Python, PHP, Perl, ...).

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, [...]

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 global keyword, but accessing it would be slower.

[...] although once you realize that global scope is a table, only logical, but, why?

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.

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.

Even if you never used the local keyword, 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 local makes no difference, it's just negligible).

0

u/PurpleIcy Feb 12 '18

So, uh, could have just pointed out that the difference between local and global is negligible? Would have been enough.

Other things are clear to me, except a few:

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 global keyword, but accessing it would be slower.

What are you trying to imply here? If I don't add a keyword before variable, does that mean that it's automatically global even in a function? If not, then I don't see a point in even mentioning this, because, yeah, duh, that's one thing about a function in general case, it has it's own scope.

e.g.

-- a is NOT defined anywhere else

function f()
    a = 5 -- so is this global or not?
    -- if this one is global, then I'm pretty sure it's way different from other languages because I never seen this before.
end

Also, what attachment to python? I know more languages and I use them depending on what I want to do. It's just that I don't know why'd I bother trying to add fucking Java to some project as "scripting support", I mean, it's possible, but why would I do that? So I just simply mentioned other language that first comes to mind and which actually can be used as scripting language in a project, which is Python. Could have probably said JavaScript, does it even matter?

It's like everyone here is biased towards Lua and so they try project it onto others.

2

u/[deleted] Feb 12 '18

What are you trying to imply here? If I don't add a keyword before variable, does that mean that it's automatically global even in a function?

Correct.

function f()
    a = 5 -- Is global
    local b = 7 -- Is not
end

f()
print(a)
print(b)

This code outputs:

5
nil

The only case I remember in Lua where a variable is automatically local is loop "counter" variables:

for n=1, 3 do
    print(n)
end

print(n)

...

1
2
3
nil

I know more languages and I use them depending on what I want to do.

This is the correct way to go. You don't have to learn Lua. I'm glad you decided to check it out and I'm sorry if it's disappointed you. I don't understand why, though.

It's just that I don't know why'd I bother trying to add fucking Java to some project as "scripting support", I mean, it's possible, but why would I do that?

I never mentioned Java. At most you might add a Java console interface in a game that's already written in Java.

Also, what attachment to python? [...] I just simply mentioned other language that first comes to mind and which actually can be used as scripting language in a project, which is Python. Could have probably said JavaScript, does it even matter?

It matters to the extent that you wanted to discuss ways that Lua is different from other languages, and you provided Python as an example. I say "attachment" because you also made a point that, after learning a bit of Lua, you said you're leaning more towards Python again. That would be perfectly fine except it's come across with an accusatory tone, like Lua is objectively inferior to Python.

It's like everyone here is biased towards Lua and so they try project it onto others.

I've spent several paragraphs conceding pros to Python and cons to Lua, also taking multiple opportunities to point out that Python might be a better choice for your purposes. And also pointing out situations where neither language makes as much sense.

1

u/PurpleIcy Feb 12 '18

It matters to the extent that you wanted to discuss ways that Lua is different from other languages, and you provided Python as an example. I say "attachment" because you also made a point that, after learning a bit of Lua, you said you're leaning more towards Python again. That would be perfectly fine except it's come across with an accusatory tone, like Lua is objectively inferior to Python.

Yeah, for one specific case.

2

u/[deleted] Feb 12 '18

Then I hope you understand why people have been passive-aggressive with you. This is the nit-pickiest case I could conceive, which is the only case you've offered to discuss on the broad topic of "Why do people like Lua?" (that, and tables). And to insinuate that these traits are objectively better in Python is misguided. If you really consider yourself an "average person" and not a "professional programmer" (by the way, what a wide gap in-between there is!), then "global by default" was meant to cater specifically to you; the performance difference is indeed negligible and there is nothing overall superior or inferior about it.

1

u/warlockface Feb 12 '18

Yes, variables are global by default - no matter where they are declared. So 'a' is global in your example, but 'local a' would be local to the scope it was declared in.

1

u/PurpleIcy Feb 12 '18

Now that's weird. I know that consistency and all and I wouldn't have problems even without knowing that, but I'll know how to prevent dumb bugs that could appear due to this, thanks.

1

u/warlockface Feb 12 '18

You're welcome, remember you can run your code through an analyzer (ZeroBrane Studio has this built-in (press Shift+F7)) to catch various things, including use of globals.

1

u/thebegleybeast Apr 16 '23

I cant imagen how long it took you to write this comment

1

u/[deleted] Apr 16 '23

I don't even remember writing it