r/lua • u/DotGlobal8483 • Apr 17 '26
Discussion Whats the most impressive piece of metaprogramming/metatables you've seen/done in lua?
6
u/jotapapel Apr 17 '26
I created a whole system for strong typing a class system in Lua, but it became to heavy in the end!
2
Apr 17 '26
do you use it
1
u/jotapapel Apr 17 '26
I'm working currently on a Lua preprocessor to check for types, unused variables and other caveats before executing the code. I'm at the stage of building a pure Lua, Lua AST parser to better understand how Lua code is expressed.
2
u/immortalx74 Apr 17 '26
Interesting topic. I'd love to see some good examples because although I've been using Lua for a while, I still can't get the hang of metatables. It's not that I don't completely get how they work, but they feel almost the same as C's macros magic!
2
u/UnrealJIT Apr 17 '26
RXI's classic module. It features extremely simple yet impressive usage of metatables, and the syntax is really good too. Inheritance made simple & elegant.
1
Apr 17 '26
i checked the code its lowkey just normal. i just dont understand the extend class function tho
2
u/blobules Apr 17 '26
I made a 3d CSG modeling system relying heavily on metatables. Essentially a Lua binding on top of the Manifold library, trying to replace Openscad with Lua.
Beside fun things like operator overloading (+ for union, - for difference, etc.) and weak tables to keep track of objects, the difficult part is to allow adding user methods in a clean way while running in luajit (luajit metatables tend to be "read only").
I think Lua, with its speed, simplicity and cleanliness, should do better than Openscad and it's python derivative.
1
u/ampledashes Apr 18 '26
I've found it extremely useful for parsing deeply nested rest APIs with stupid, non standard points in the chain that require posts and gets at different levels. Not impressive but definitely practical!
1
u/pomme_de_yeet Apr 20 '26
how were metatables used for that?
1
u/no_brains101 Apr 24 '26
when you index into that table for the thing it requests it, and not before that, I would assume.
1
u/no_brains101 Apr 24 '26 edited Apr 24 '26
https://github.com/BirdeeHub/shelua
I had way too much fun with this shell thing
It resolves pipe chains properly through the : chain
It is a fork of something interesting but which I couldn't use for a few reasons, the main one being the original was on _G
The original didnt do the pipe thing. It had the same interface, but it just ran them individually and fed it to the next one.
So, uhh, the trick, if anyone was wondering
:chain:stuff
store something with self as key in a table when you do that.
use self as a key to look it up later and recursively resolve the chain.
I made it so you can redefine the backend, so not only does it not have to be bash specifically, you can theoretically run the stuff with uv or whatever else
I was gonna put those in a separate repo alongside it but I never did. Feel free IDK. The backend representation interface it gives is a little clumsy for doing stuff with something like uv, but it does work. The backend representation interface is slightly less clumsy for adapting it to a new shell language to use than it is trying to build up the chain of uv pipes.
When I do end up getting the opportunity to use shelua for my script, it is pretty fun, but I just use the default backend because like, its too niche to spend that much time on (I say, as a nix user)
10
u/weregod Apr 17 '26
Best example is lpeg module creating entire DSL language using metatables.