There’s a special place in hell reserved for whoever invented metatables.
I’ve just spent the best part of two days tugging at my flaxen locks trying to work out why the table returned by the __index of a metatable was being updated by indirect access instead of throwing an ‘attempt to index nil’ error for the public table when setting a value like this:
-- this table has no value for index (nil), but a metatable -- with an '__index' method to return a default value table_with_metatable[index].value = something -- expect that it would complain of no field 'value', but instead -- sets the metatable[index][value] variable to 'something'
So, it’s fetching the table at __index and then the dot operator is just sugar for [ ] on the table that lives at the index of the metatable. How infuriating. It makes somewhat more sense as i write it out here, but maybe i’m just stupid and this is obvious to everyone else. Zeus strike me down if i don’t find C++ references and pointers much easier to deal with. Just give us a ‘read only’ flag to set already rather than having to bloat out the code with loads of guff to deal with this insanity!! I just want to shadow a table of default values by setting and removing them in the top level of the code, it’s not much to ask.