I am currently working on luna, an OOP library for lua.
At the moment though, I’m having a bit of a headache trying to ensure it works with ROBLOX since the debug library is locked down. I want to be able to check if a function is part of the definition (which is possible with setfenv) but then further want to check if the first argument passed to that function (aka self) is the same as the one my metamethod has been invoked on, allowing users to access the classes private scope.
To do this I would need access to the following two methods. getinfo and getlocal.
I know the debug library is quite a powerful tool, but it would be really useful if we could access it.
Every feature request should be in a category; this belongs in #feature-requests:client-features
That being said…
getlocal would be incredibly useful to me. I’ve had to resort to hacky methods to get local variables from a scope before, and it’d be nice if that weren’t a thing I had to do. May as well go the whole distance and give us debug.setlocal and debug.getupvalue/debug.setupvalue.
If you’re trying to get locals from another scope, you’re probably doing something wrong.
In your case, you might want to have a local at the top of the class for private access. Or you just wrap(self) with a proxy that allows for private access inside for methods. OOP in Lua is tricky, but using debug doesn’t sound like a good solution
If the library was solely designed for my use then this would not be a problem. At the moment I am using a proxy as you have suggested but this is far from ideal. If a user wanted to make a parentable class then they could end up pushing the proxy version outside of the definition which would not end well.
Furthermore class syntax is as follows
local list = class {
__construct = function (this, ...)
this.items = {}
end,
__index = function (this, index)
return this.items[index]
end,
__newindex = function (this, index, value)
this.items[index] = value
end,
Count = {get = function (this) return #this.items end},
IndexOf = function (this, item, n)
for i = n or 1, #this.items do
if this.items[i] == item then
return i
end
end
end,
Add = function (this, item)
table.insert(this.items, item)
end,
Remove = function (this, item)
table.remove(this.items, this.IndexOf(item))
end,
RemoveAt = function (this, index)
table.remove(this.items, index)
end,
ToArray = function (this)
return this.items
end
}
I’m not entirely sure how the former would be achieved in this situation.