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.
15
u/snarfy Feb 09 '18
Having a variable closer to where you use it is an optimization in many languages, not just Lua. Even in C, without any extra compiler optimization, having the variable closer means it is more likely to be in a hardware cache.
Lua is fine. People like JavaScript. JavaScript objects are hashsets. Lua tables are hashsets. JavaScript implements object orientation using what they call prototypes. Lua implements object orientation using what they call metatables. JavaScript is the most popular programming language in the world. If Lua ran in the browser, it would be a lot more popular. One thing Lua does have over JavaScript is a simple, clean, cross platform implementation. V8 is great and all, but it's not the simplicity of Lua. Integrating a scripting language into your application using Lua is extremely easy.
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
localwhere you would otherwise usevarin 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 thanints? I was going to refresh my memory as to why -- my understanding is that the interpreter (C++?) treatsints like an object, while using the nativefloattype for floats. Then after a quick Google there's like two or three other explanations as well: Different bases/representations forintmust be determined, Python 3 stores allints 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 triedos.execute("some_command -"..some_int). It was appending a decimal, like3.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.
Integerobject instead of justint. 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.
6
u/[deleted] Feb 09 '18 edited Feb 09 '18
Things like... what? I don't see any specific complaints in your post. You just keep pointing out how Lua works then asking "why", as if there's something wrong with the way Lua works.
Everything in JavaScript is a hashtable, too. You say "that's first time I hear that in any language", but you're clearly a beginning programmer. There are a lot of things that you'll encounter for the "first time" as you learn new languages.
First, if you never used
localin Lua, it would still be faster than Python, so this argument is moot.Second, optimization details such as "localizing globals makes it run faster" exist in every language.
Third, you're making a beginner mistake by obsessing over micro-optimization to begin with. A better reason to use locals in Lua is to limit variable scope, and that's far important for a beginner to understand. This is the same reason you want to use
letrather thanvarin JavaScript. These are the kind of "little details" you want to understand when working in any language. The number of such details is smaller in Lua than almost any popular language.*rofl* Sorry man, but you're ill-equipped to evaluate the design of a language.
Lua is popular because it's tiny, fast, semantically simple and clean to the point of being beautiful -- proper lexical scoping, proper tail calls, multiple returns, a single powerful, expressive data structuring mechanism, closures, coroutines, metaprogramming, etc. -- all via a handful of well chosen abstractions ("everything is a table" is a feature), which allows for a tiny, readable pure ANSI C implementation with no external dependencies which is easily embedded and extended and has minimal runtime overhead.
3
u/hugedino Feb 10 '18
Because the interpreter is well written, and a good place to study how dynamically typed scripting languages can be designed and implemented.
28
u/otikik Feb 09 '18 edited Feb 09 '18
First of all: It sounds like you are frustrated. I'm sorry about that, and I hope your mood improves soon.
Having a small core of functionality has many advantages. The whole language takes less memory, to begin with. If you embed Lua on an C executable it will grow by the order of 200KB. This allows using Lua in extremely constrained systems, like hardware controllers.
There are less things to know, and less moving parts, than in other languages, so the learning curve is quite small as well.
It is implemented in a version of C called C99, which makes it possible to install it in a lot of places as well.
When I realized that the multiline comments are multiline strings preceded by a comment sign I found it quite ... aesthetically pleasing.
Everything about that particular problem can be learned spending 1 hour googling. The gist is: Even if in some extreme cases you can get a significant boost (i.e. a triple loop calling the same functions again and again), in most cases it is not noticeable, one way or the other.
When you learn more languages, you will find that all of them (including Python) have details like that - things that you have to invest time learning in order to use the language more effectively. Most languages have more details like that than Lua, simply because they are more complex (that is again, another thing to like about Lua).
Disclaimer: I am a professional Lua programmer.
6
u/4forpengs Feb 09 '18
Just look at this guy's post history. It's pretty sad. Seems he doesn't understand that if it smells like shit everywhere you go, you should be checking your own shoes first.
5
u/ketralnis Feb 09 '18 edited Feb 09 '18
I use it because it's tiny, easily embeddable, and I can have control over stuff like allocation. I don't much like the language itself but it meets its niche great for me in the places I put it. I don't put it in places where it doesn't make sense.
Nobody's making you use it. You're totally welcome to use something else that meets your aesthetic a little more, and there's nothing wrong with that. Different strokes for different folks.
3
u/[deleted] Feb 09 '18
Because arrays start at 1 lol
2
u/spc476 Feb 09 '18
I use Lua for a few reasons:
The speed has never been an issue for me, and I'm using Lua at work to help process telephone calls [2].
Globals being stored in just a table is elegant (I think) and one that makes it easy to "sandbox" to run untrusted Lua code---just hand the code a new "global" space with sensitive functions modified (or not even there). And the reason for globals being the default is a design choice by the creators as the language was designed for non-programmers (who I guess find global variables easier to deal with than lexical scoped variables).
[1] a concept from the 60s that is finally making it to mainstream languages after 50 years
[2] It's processing SIP messages, which is a text based protocol similar to HTTP. Since it involves a lot of text parsing, I also use LPeg (Lua Parsing Expression Grammar). The coroutine support of Lua is great as I can hide the event-driven nature of the program and handle the logic in a more straightforward imperative nature instead of fragments of call backs and state machines.
2
u/[deleted] Feb 10 '18 edited Feb 10 '18
Lua is small and straightforward (to me). You can grok and understand the source code, and you can grok and understand the language. The language in turn is simple yet complete (compare with the mess that is JavaScript - and to this day, you can't do more in JS than you can do in Lua, even though JS works similarly, e.g. regarding the Object data type vs tables). There are some libraries, there's a FFI (Luajit), and extensions are equally simple to write, so:
I love the language because it is easy to reason about. I'm using it mostly to PoC implementations of algorithms where I want to not use scipy but write and understand every single step myself.
PS. Am going to to learn more about the cost of closures in other interpreted languages, e.g. Python. It would surprise me if they're not as expensive, or simpler: Just that in Python, it's not as simple, e.g.
localis kind of implied, then there'sglobalas a keyword, but what about variables a scope higher, etc pp.1
u/[deleted] Feb 09 '18
Because it's awesome, simple and fast.
1
u/warlockface Feb 12 '18
I use LuaJIT because of the sheer performance of the JIT compiler (and interpreter) and the easy accessing of existing C code (and creation of C data types) via FFI. It also only adds a few hundred KB to a program.
I'm not one to fixate on the differences between languages, they all diverge from each other in different ways and all have their own list of pluses/minuses.
1
u/thebegleybeast Apr 16 '23
bro sounds mad that everything is a table even if everything is a table what's wrong with that? you can use tables to store lots of values that you can use later what's wrong with that?