Which talks about how you can combine two things, like metatables. It doesn't really combine them here. It puts them on a list and if a result was found, it caches it for faster access, directly into the table _(not metatable, because it doesn't have one)_ itself.
And I was trying to think, if there would be the possibility to add a feature to Lua itself.
First, I've been exploring methods. And I found 3.
If you have any suggestions, let me know.
- __index chaining
- Is the worst one, since I think if you chain too much it thinks it's a loop and might throw an error, depends who is configuring their Lua VM. And the further back something is, the longer it takes.
- search function
- Is good, but the further something is on a list the longer it takes, unless cached. But all values are inherited and referenced. Unless you cache it, then it's lost.
- Mapping
- Mapping, is a concept that I found.
Basically, it creates a metatable, which is the _proxy_, where all the keys from the metatables are stored inside of the _router proxy_ and point towards the tables... The issue is, this works for functions. Because you can do
mergedMeta[k] = function(...) return parentMeta[k](...) end.
But "Mapping", creates a new table, which uses memory and also creates anonymous functions. You could create a metatable and mix it with __call and then denote the parent table with weak modes, for more interesting caching. But non-functions will have an issue here.
Theorizing a new feature
Asking the purpose of a metatableWhy do metatables even exist? I think they might only exist, to reduce the re-creation of tables, by binding a table and saving memory. But you can only do it once.
Doing it more than once to support OOP inheritance, seems to sound critical.
Technically, if pure Pointers would exist in Lua, it would fix the issue, but maybe it's too complex to implement GC for it.
The thing is, isn't a table already something that actually points to the actual table nodes?
But I was thinking of a table function. That table function, would combine two table pointers into one...
I haven't studied Lua's table source code. But it probably wouldn't work so easily. I don't know how table's reserve a space in the Lua Environment to dynamically adapt when adding something inside a table.
But this magic exists, what if there would be a way to store 2 table's keys and value as pointers into another table?
Where same keys would get overwritten. It does create a new table, but that table is just a router to the actual values. Which would make it possible to efficiently have multiple inherited tables, like metatables of a kind.