r/lua • • Nov 01 '25

Does anyone else use lua as their main language for system automation and shell scripting?

it's no secret that Lua is heavily underrated. ironically, i used to be against lua until i understood how nifty it is as a language. i formerly thought that one had to use index or metatable to create objects, only to find out that i could do this

```

function NameOfObject(instanceVariable1, instanceVariable2)

return{

method1 = function(self)

end,

method2 = function(self)

end,

method3 = function(self)

end,

}

end

```

and just like that it overtook the appreciation i had for how objects can be created in other languages.

in a less subjective sense, however, given how ridiculuously fast lua is compared to the bulk of languages that are interpreted, i committed to it for all my scripting needs.

lua has now become my premier choice for automating anything related to my environment: my file system, my window manager, and even my system settings. it also greatly helps that i can automate repetitive and tedious text editing in neovim with lua as well.

i formerly used python for my scripting needs, and in retrospect, it's rather comical to me how poor python is as a scripting language compared to lua; it's a night and day difference. i can always trust lua for a startup time that is as good as instantaneous, whereas such a thing would be a fool's game in python.

granted, python does have all the abstractions one can hope for, but for system tasks, it's just outright overkill. one does not need a gazillian abstractions for system admin or shell scripting needs

anyways, i've rambled long enough. is there anyone else that uses lua for shell scripting needs and system automation, as opposed to roblox, C programming, game dev, and other low level tasks?

75 Upvotes

61 comments sorted by

View all comments

Show parent comment

1

u/SwimmingPermit6444 Nov 10 '25 edited Nov 10 '25

There's several reasons to prefer the typical pattern over your approach, other than inheritance.

Your approach

  1. Has more memory overhead
  2. Is less performant during object construction
  3. Loses out on metamethods
  4. Cannot be hot patched

Memory overhead

With the typical metatable + __index, methods for a class live in a single table, not stored inside each instance of the object. And yes, they really are different function objects:

function NameOfObject() return {
  method1 = function(self)
    print("")
  end
}end

local a = NameOfObject()
local b = NameOfObject()

print(a.method1 == b.method1) --> false

Whereas for the typical pattern, methods are truly shared, avoiding duplicate function objects per instance:

local Foo = {}
Foo.__index = Foo

function Foo.new()
  return setmetatable({}, Foo)
end

function Foo:method1()
  print("")
end

local a = Foo.new()
local b = Foo.new()

print(a.method1 == b.method1) --> true

Construction Performance

With your approach, each method is a new field assignment, whereas with the typical approach, it's a single call to setmetatable.

Metamethods

When defining your own data types, it's often helpful to use the built in operators such as + to operate on your objects. There's other useful metamethods too. With your approach, you don't get to use any meta methods.

Hot patching

Sometimes you might want to change or add a method at runtime. With the typical approach you can change the prototype. With your approach, you can't do this easily. You'd have to change each object individually.

So for simple scripts with just a few objects where hot patching is not necessary, your approach can work. It's main advantage is simpler conceptual overhead- there's just tables with data and functions in them, no shared table to complicate things. For projects where there could be thousands of objects, or defining datatypes such as vectors where metamethods are super handy, the typical pattern starts to make more sense.

1

u/AutoModerator Nov 10 '25

Hi! Your code block was formatted using triple backticks in Reddit's Markdown mode, which unfortunately does not display properly for users viewing via old.reddit.com and some third-party readers. This means your code will look mangled for those users, but it's easy to fix. If you edit your comment, choose "Switch to fancy pants editor", and click "Save edits" it should automatically convert the code block into Reddit's original four-spaces code block format for you.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/kayinfire Nov 10 '25

well done counterargument i must say. i appreciate you highlighting the suitability of my approach for smaller-scale programs. before arriving at that section, i was quite honestly starting to think you missed the part of the post where i said

lua for shell scripting needs and system automation, as opposed to roblox, C programming, game dev, and other low level tasks

i concede that for those realms, the advantages you speak of would indeed render the "metatable and index" approach the appropriate one.

nevertheless, the justification you provided for the typical pattern really goes to show why i made the post to begin with. in my experience, it is commonplace for one to use Lua for scenarios where the highest priority is given to performance. everyone uses Lua for those things. conversely, it is immensely rare that i see anyone using lua for personal programs or programs where readability is more desirable than performance, and your response in addition to the fact that the metatable approach is the prevailing approach testifies to that. the impression i get is largely that most people almost exclusively concern themselves with efficiency when it comes to lua. to a fair extent, this can't be helped considering the historical origins of lua and its marriage to embedded systems. however, i'd rather use lua as a personal scripting language on the grounds that i regard it as aesthetically pleasing and immensely performant relative to the alternatives, even with my approach. i suppose i am unorthodox in this regard

1

u/SwimmingPermit6444 Nov 10 '25

Right, sometimes with Lua I manage games with hundreds of game objects being simulated at once. Performance starts to matter at that point. I have heard of your way of doing things and just wanted to point out some potential downsides to anyone passing by. I definitely didn't mean to criticize, it's a good approach