Theorizing a new built-in function? Multiple Inheritance

64 views
Skip to first unread message

Karl Police

unread,
Jun 23, 2024, 12:13:02 PM (10 days ago) Jun 23
to lua-l
There's this thread about Multiple Inheritance https://www.lua.org/pil/16.3.html

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 metatable
Why 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.

H J

unread,
Jun 24, 2024, 8:40:28 AM (9 days ago) Jun 24
to lu...@googlegroups.com
Isn't this what the classy package is doing already? (https://github.com/siffiejoe/lua-classy)

--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lua-l/5bc78ea3-bf1a-45d9-a6ad-6533c1724fbfn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages