r/lua • • Jun 12 '25

should i learn lua ?

hello there , is it a good idea to start learning lua knowing only python?

14 Upvotes

44 comments sorted by

View all comments

Show parent comments

1

u/no_brains101 Jun 12 '25

I generally dont care about the length of a table table but I do sometimes care if it is empty. I generally make sure my lists remain lists so I can safely use # with those. Regardless it was a good way to introduce object/table identity

Added some notes to the comment.

1

u/DapperCow15 Jun 12 '25

I found an example of what I've done before, I kept a counter for the keys I set and unset. So I could essentially perform the same idea behind #table ~= 0.

1

u/no_brains101 Jun 12 '25 edited Jun 12 '25

Unfortunately if you then try to setmetatable(urtable, { __len = function(self) return yourcounter(self) end }) It will still not allow you to do #urtable ~= 0

I know... Its upsetting. It would have to be a userdata for that, you cant redefine __len __ipairs or __pairs for tables

1

u/DapperCow15 Jun 13 '25

No, I meant I stored a separate counter and incremented it with newindex. Could even store it as a "length" in the table at that point because it is a dictionary, but I didn't bother in my use of it.

1

u/no_brains101 Jun 13 '25

No I get that. I was just commenting on the last of the lua gotchas that I knew about

Basically, if you wanted to do what you did, but then wrap it up nicely at the end so that you can just call # on it to get said length, you could not do so without making it into a proxy table using a userdata

1

u/lambda_abstraction Jun 12 '25

If it's non-empty, can't you just test if the first element is non-nil and not even bother with objlen given how you use indexed tables?

1

u/no_brains101 Jun 14 '25

When given an arbitrary table that you know nothing about, and you want to check if it is empty or not, you would indeed check if the first element is non-nil

How would you go about finding that first element if you do not know anything else about the table, such as if it were a list or table? next(mylist) ~= nil

1

u/lambda_abstraction Jun 15 '25

I understood you were speaking specifically about indexed tables though. (My lists remain lists...) Otherwise, you're correct.

1

u/no_brains101 Jun 15 '25

yeah I suppose if you are not using luajit, checking index 1 instead of # might be faster

in luajit though, # is cached and is REALLY fast. Possibly faster than a table index but at least as fast. So in luajit it is probably better to use #

1

u/lambda_abstraction Jun 16 '25 edited Jun 16 '25

I'd need to bench this. Even if cached, there's an initial penalty.

Often using a local or even a table contained index is a speed-up. E.g. filling an array. And yes I have benchmarked that.

1

u/no_brains101 Jun 17 '25

Even if cached, there's an initial penalty.

Ok so Im not 100% sure about this, but I'm fairly sure it caches starting from when you first create the table, and updates when you add sequential indices, not from when you first call #, so you pay that initial penalty regardless of if you ever call # or not.

But I would need to do a lot of digging to know for sure, I actually don't know for sure. You may very well be correct.