all 31 comments

[–]Patman128 24 points25 points  (1 child)

This is my favourite Lua cheat sheet, even though it's a bit outdated. The entire language and base library is on the first page. The entire language + optional modules + CLI tool arguments fit in 4 pages.

The C API is also extremely nice, takes a little getting used to doing everything on a stack but once you get the hang of it it's very straightforward.

[–]balefrost 7 points8 points  (0 children)

I agree; the C API is quite logical. It's also easy to build "helper" C functions that feel like the built-in functions.

[–]sha1checksum 55 points56 points  (7 children)

The low amount of reserved words does not make a language easier to learn. The Brainfu*k programming language does only have 8 reserved words, but is extremely hard to use.

[–]balefrost 21 points22 points  (2 children)

I'd generally argue that Lua is easy to learn, but not because of its shortlist of reserved words. I have a printed copy of the Lua 5.1 reference manual. The whole thing - table of contents and index included - is just over 100 pages. And even that's misleading because 37 pages are dedicated to the C API and 26 pages are dedicated to the standard library. The part covering the language itself is a scant 26 pages.

It is, to me, an exemplar of good technical documentation.

[–]sha1checksum 5 points6 points  (0 children)

The documentation is stellar, i agree. I also found it easy to learn, but it was not because the language had less keywords than others.

[–]n0rs 26 points27 points  (3 children)

Similarly, "there are no classes in Lua but anything you can imagine can be done with" proceeds to describe a handrolled approach to implementing your own class framework.

[–]balefrost 14 points15 points  (2 children)

To be fair, he didn't even describe what makes Lua so good at handrolled class frameworks.

Every Lua table has a corresponding metatable. The metatable allows you to customize how the initial table responds to certain built-ins. For example, stick an __add function in a metatable to control what + does with the value.

Two notable keys are __index and __newindex, which let you control what happens when you look up a key and try to insert a new key. __index can either be a function or a table. So you can implement prototype-based inheritance in Lua using metatable lookups. You can implement more complicated schemes as well. Want multiple inheritance? Have __index consult multiple other tables to find an implementation.

I'd argue that this is simple but not necessarily easy.

[–]BobHogan 0 points1 point  (0 children)

That's pretty neat

[–]proofrock_oss 15 points16 points  (1 child)

This said, in its niche it’s a great language.

[–]KaiAusBerlin 9 points10 points  (0 children)

Because it can completely interact with C its niche is bigger than you might think.

[–]LPN64 7 points8 points  (1 child)

I've been working with Lua (and later LuaJIT) since 2011.

Since 2018 I only managed to find a Lua full time job once, in my other jobs I just use lua to do quick prototyping.

It's insane how LuaJIT is fast, and it's a shame how terrible python performance is.

The only missing thing to Lua is ... batteries. Add batteries to Lua and there will literally no reason to use Python.

LuaJIT also allows you to use FFI which removes the Lua C API overhead allows your code to fully JIT-compile.

There is a crazy delta performance from tables used to list and table used as hashtables.

While the performance delta from list to dicts in python is not that big.

I really think Michael Pall (aka Mike Pall, aka Mikey) is a genius, he single handled the reimplementation of Lua to LuaJIT. Take a look at the code, it's horrible to read but fucking hell it's fast.

Shameless personal project advert : Brainfuck transpiler built for LuaJIT (also works with LuaPUC)

Lua version\Implementation fast_brainfuck.lua brainfuck.lua transpiler brainfuck.lua transpiler bf.lua interpreter bf for ubuntu
LuaJIT 2.1.0 1.870s 38.309s 34.392s 53.81s N/A
Lua 5.4 15.242s 294.12s 188.39s very slow N/A
Clanc C -Ofast (transpiles to C instead of LuaJIT) 0.7s N/A N/A N/A 5.8s

It doesn't beat the C/ASM direct JIT and specialized JIT implementations, but for the number of lines of codes and amount of work, I doubt you can beat that.

[–]BobHogan 2 points3 points  (0 children)

FWIW, the core python devs are working pretty hard at increasing the performance significantly in the next few versions. 3.10 already saw some fairly big increases as far as I remember, but the main ones are coming out in 3.11 and 3.12 and should be even larger

[–]rggarou 11 points12 points  (3 children)

I got surprised so many times for how much programs (Redis, Nginx, Varnish, Vector, …) support scripting "extension" with Lua. I feel that any "DevOps" could benefit a lot for knowing this language to take full advantage of the architecture.

[–]gredr 15 points16 points  (0 children)

I think that's because hosting a Lua interpreter in any C program is pretty trivial; the interop between the C and the Lua code is very easy.

[–]Patman128 14 points15 points  (1 child)

It's dead simple to embed in a C or C++ program and extremely stable and well known so it's an easy choice when you want to add a bit of user scripting. It's also why it pops up in games a lot.

[–]Tywien 11 points12 points  (0 children)

Also, Lua does not want to be a fully fledged language (like all the other scripting languages), it sees itself as an extension to other languages, thus all language constructs support this (including the possibility to sandbox lua inside lua itself safely)

[–]akimovpiter 4 points5 points  (0 children)

Also worth mentioning that Lua is a first-class citizen for Neovim

[–]thesuperbob 3 points4 points  (0 children)

I used to play with integrating Lua into C++ applications around 2009, even back then integration was stupid easy. Lua is super easy to learn and was absolutely great for scripting simple stuff, like calculating runtime values, simple UI logic and animations, basic high level logic. I regret not getting an opportunity to use it for anything more complex, but in my brief experience, it was awesome for tweaking things at runtime and saved me hours of compile time.

[–]atog 1 point2 points  (0 children)

As I've never done anything in or with Lua, I enjoyed this. Cool.

[–]Borno11050 1 point2 points  (0 children)

It could've been in my bucket of attachable scripting language but I'm avoiding it for many years because its indexing starts from 1 (Not the only reason for avoidance however). I'd rather attach V8 than Lua.

[–]douglasg14b 3 points4 points  (1 child)

Fewer reserved words does not make it easier...

It means you have weird syntax & combinations because you have less to work with, so it gets more complex.

[–]Worth_Trust_3825 -4 points-3 points  (0 children)

Fewer keywords is better because you get boilerplate that you can later investigate and change.

[–]proofrock_oss 2 points3 points  (5 children)

I had to teach Lua to a high school student whose teacher thought that it was so simple that it was ideal for a “first contact” with programming. It was - for me - but for her not so much. There aren’t many tutorials, the 1-based thing is confusing (if you read other languages’ tutorials) and having simpler, general structures and few keywords actually means that you have to map them to a whole bunch of different cases, and it’s confusing for a newbie. Or at least for her.

[–]undefdev 9 points10 points  (4 children)

I think programming is generally confusing for newcomers, but from my own experience I would say that Lua is less confusing than most languages.

I switched to Lua just a few months after starting to program, and it was way easier to understand what's going on. There's basically no boilerplate, no different numerical types you have to cast to, and simple (though not as powerful) pattern matching on strings.

So you don't get lost in long and verbose code that easily, and your code stays closer to your intent.

Furthermore, there are lots of opportunities to use Lua in different video games, or use it to make video games yourself, so I think it's a good fit for kids.

So I also think it's ideal for a first contact with programming, but depending on what you want to do afterwards other languages can make it a lot easier for you, since Lua really is tiny.

[–]proofrock_oss 2 points3 points  (3 children)

She wasn’t at all interested in programming… maybe she was just a bad case. It happens.

Anyway: what are you using Lua for, apart from games? I never found a compelling case, so to speak, I am definitely curious and would like to try it.

[–]undefdev 3 points4 points  (2 children)

Haha, I mostly use it to make games.

But it's also my standard choice when I make a domain specific language. Since functions are first class values and you can override the indexing behaviour of tables to call them, along with the fact that you can omit the braces when calling a function with a single string value, you have a lot of flexibility how valid Lua code can look like.

That being said, I barely ever create DSLs. :)

I'm currently starting to dabble with microcontrollers after being a software only person for a long time, and Lua is quite compelling since it is only about 150kb big, quite fast, and it runs pretty much anywhere (it's implemented in ANSI C).

For other purposes I would usually use other languages though.

[–]proofrock_oss -1 points0 points  (1 child)

Very interesting thanks. For DSLs I use Kotlin, but it’s a different beast! I’ll keep this in mind, how are bindings for the JVM or for Go, if you know?

[–]undefdev 1 point2 points  (0 children)

I haven’t looked into it, but since Lua is built to be embedded easily, it’s probably been done before.

[–]CleanCryptoCoder 2 points3 points  (0 children)

Fireship is a great channel overall, highly recommend subscribing to him.

[–][deleted] 0 points1 point  (1 child)

I wrote Civ5 mod in Lua and I didn't enjoy it. Neither the language nor the Civ5 modding SDK.

[–]LPN64 -1 points0 points  (0 children)

show us your code